TypeError: Cannot convert undefined or null to object in JS

avatar
Borislav Hadzhiev

Last updated: Mar 4, 2024
4 min

banner

# TypeError: Cannot convert undefined or null to object in JS

The "Cannot convert undefined or null to Object" error occurs when we pass a null or an undefined value to a function that expects an object as a parameter.

This commonly happens when using the Object.keys(), Object.values() and Object.assign() methods.

cannot convert undefined or null to object

Here are some examples of how the error occurs.

index.js
// โ›”๏ธ TypeError: Cannot convert undefined or null to object // ๐Ÿ‘‡๏ธ passing null to Object.keys() Object.keys(null); // ------------------------------------------------ // ๐Ÿ‘‡๏ธ passing undefined to Object.values() Object.values(undefined); // ------------------------------------------------ // ๐Ÿ‘‡๏ธ passing undefined to Object.assign() Object.assign(undefined, {}); // ------------------------------------------------ const obj = undefined; // ๐Ÿ‘‡๏ธ trying to delete a property on an undefined/null value delete obj.property;

Passing a null or an undefined value to a method that expects to get called with an object causes the error.

# Check if the value of the variable is truthy before calling the method

To solve the error, conditionally check if the value you're passing to the function is truthy.

The function will not be invoked if the value is null or undefined.

index.js
const obj = undefined; if (obj) { const keys = Object.keys(obj); } else { // ๐Ÿ‘‡๏ธ this runs console.log('โ›”๏ธ Object is falsy'); }

check if value of variable is truthy before calling method

The code for this article is available on GitHub

Our if statement checks if the variable is truthy before calling Object.keys().

This way, we won't call the method with a null or undefined value.

The list of falsy values consists of: false, null, undefined, 0, "" (empty string), NaN (not a number).

All other values are truthy.

# Using the typeof operator if the variable might not be declared

If the variable might not be declared, use the typeof operator instead.

index.js
if (typeof obj !== 'undefined' && obj !== null) { const keys = Object.keys(obj); } else { // ๐Ÿ‘‡๏ธ this runs console.log('โ›”๏ธ Object is falsy'); }

using typeof operator if the variable might not be declared

The code for this article is available on GitHub

The typeof operator returns a string that indicates the type of a value.

The obj variable isn't declared in the code sample, but the typeof operator doesn't throw an error, instead, it returns undefined.

The if block only runs if the obj variable is declared and not set to undefined or null.

# Solve the error using the logical OR (||) operator

Alternatively, you can use the logical OR (||) operator to provide an object as the backup value.

index.js
const obj = undefined; const keys = Object.keys(obj || {}); console.log(keys); // ๐Ÿ‘‰๏ธ []

using logical or operator to solve the error

The code for this article is available on GitHub

The logical OR operator checks if the value to the left is truthy and if it is, it returns it.

In all other cases, it returns the value to the right.

undefined and null are falsy values, so the logical OR (||) operator returns the value to the right-hand side (the empty object).

This covers us in the scenario where the value is null or undefined.

# Make sure you aren't trying to delete a property on undefined or null

The error also occurs if you try to delete a property on an undefined or a null value.

index.js
const obj = undefined; // โ›”๏ธ TypeError: Cannot convert undefined or null to object delete obj.property;

You can use an if statement to make sure the variable doesn't store a falsy value before using the delete operator.

index.js
const obj = undefined; if (obj) { delete obj.property; }
The code for this article is available on GitHub

The code sample checks if the obj variable stores a truthy value before using the delete operator.

You can also explicitly check if the variable doesn't store null or undefined.

index.js
const obj = null; if (typeof obj !== 'undefined' && obj !== null) { delete obj.property; }

The if block is only run if the obj variable doesn't store an undefined or null values.

If you need to remove a property from all objects in an array, check out the following article.

# Solve the error using the ternary operator

You can also use the ternary operator to solve the error.

index.js
const obj = undefined; const keys = obj ? Object.keys(obj) : []; console.log(keys); // ๐Ÿ‘‰๏ธ []
The code for this article is available on GitHub

The ternary operator is very similar to an if/else statement.

If the expression to the left of the question mark is truthy, the operator returns the value to the left of the colon, otherwise, the value to the right of the colon is returned.

If the obj variable stores a truthy value, the call to the Object.keys() method is returned, otherwise, an empty array is returned.

Want to learn more about working with null and undefined in JavaScript? Check out these resources: Remove Null or Undefined Values from an Array in Javascript,Remove Null or Undefined Values from Object in Javascript.

# Solve the error by using the nullish coalescing (??) operator

You can also use the nullish coalescing (??) operator to solve the error.

index.js
const obj = undefined; const keys = Object.keys(obj ?? {}); console.log(keys); // ๐Ÿ‘‰๏ธ []
The code for this article is available on GitHub

If the value to the left of the nullish coalescing operator (??) is equal to null or undefined, the value to the right is returned, otherwise, the value to the left of the operator is returned.

If the obj variable stores a null or an undefined value, the Object.keys() method is called with an empty object, so no error occurs.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev