If you are encountering the error: “TypeError: Cannot convert undefined
or null
to Object” in JavaScript and have difficulty in resolving this error. Let’s follow this guide with the explanation and examples below to solve it.
What is TypeError in JavaScript?
The TypeError in JavaScript occurs when you apply a specific operation to the value of Type and it’s incorrect.
Look at the example below to learn more about this error.
var x console.log(x.length) // This Error occurs here.
Output
TypeError: Cannot read properties of undefined (reading 'length')
This error occurs when you call the length()
function but variable x
does not have this function.
After learning more about how TypeError in JavaScript happens, you will move to learn how to solve the error: TypeError: Cannot convert undefined
or null
to object In JavaScript
How does this error happen?
This error occurs when you try to operate a function with the parameter is null
or undefined
.
Look at the example below to learn more about this error.
var x = null console.log(Object.keys(x)) // The Error occurs here.
Output
TypeError: Cannot convert undefined or null to object
Solve The Error “TypeError: Cannot convert undefined or null to object”
Solution 1: Check if the value is null or undefined before operating the function
You can check if the variable is null
or undefined
before operating to avoid encountering this error.
Look at the example below to learn more about this solution.
// Create a function to check if the variable is null or undefined and handle it. function myFunc(x) { if (x == null || x == undefined) { console.log('The Error will occur.') } else { console.log(Object.keys(x)) } } var x = {"hello": 1} var y = null var z = undefined // Now, try this function with the variable 'x'. myFunc(x) // Try this function with the variable 'y'. myFunc(y) // Try this function with the variable 'z'. myFunc(z)
Output
[ 'hello' ]
The Error will occur.
The Error will occur.
Solution 2: Use try-catch method
You can solve this problem with the try-catch
method in JavaScript.
Look at the example below:
// Create a function to check if the variable is null or undefined and handle it. function myFunc(x) { try { console.log(Object.keys(x)) } catch (error) { console.log('The Error will occur.') } } var x = {"hello": 1} var y = null var z = undefined // Now, try this function with the variable 'x'. myFunc(x) // Try this function with the variable 'y'. myFunc(y) // Try this function with the variable 'z'. myFunc(z)
Output
[ 'hello' ]
The Error will occur.
The Error will occur.
Summary
To solve the TypeError: Cannot convert undefined
or null
to object in JavaScript, you can check if the value is null
or undefined
before operating the function or using try-catch
method in JavaScript. Choose the solution that is the most suitable for you. We hope this tutorial is helpful to you. Have an exciting learning experience. Thanks!
Maybe you are interested:
- Cannot find module ‘@babel/core’ error
- Cannot set property ‘innerHTML’ of Null in JavaScript
- Cannot convert undefined or null to Object in JavaScript
- To load an ES module, set “type” – “module” in JavaScript
- How to Rename an Object’s Key in JavaScript

My name is Thomas Valen. As a software developer, I am well-versed in programming languages. Don’t worry if you’re having trouble with the C, C++, Java, Python, JavaScript, or R programming languages. I’m here to assist you!
Name of the university: PTIT
Major: IT
Programming Languages: C, C++, Java, Python, JavaScript, R