What is "Cannot convert undefined or null to object" #
This error commonly happens in these few conditions:
- Passing "null" or "undefined" arguments into Object.keys() function.
- Passing "null" or "undefined" arguments into Object.values() function.
- Passing "null" or "undefined" arguments into Object.assign() function.
- Delete a property on "null" or "undefined" value.
Let's see an example:
const value = undefined;
Object.keys(value);
Object.values(value);
Object.assign(value, {});
delete value.property;
This is the output:
TypeError: Cannot convert undefined or null to object
All of the examples will output the same error. But why? Let's look at next section.
Solutions #
Well, this error can be fixed very easily with few simple solutions. Let's go through them together.
Solution 1: Use 'if' statement to Check Truthy / Falsy #
const value = undefined;
if(value){
Object.keys(value);
}
After implementing this solution, you may notice that the error has gone because "undefined" value is a falsy value (evaluated to be false) and hence the codes within the `if` statement will not be executed.
Solution 2: Use Logical Operator OR (||) #
const value = undefined;
const keys = Object.keys(value || {});
console.log(keys)
This is the output:
[]
Why? Because we've passed an empty object as a backup value. Logical Operator OR (||): If the left operand is evaluted as false, then the right operand will be returned as a value. Since we're passing an empty object without any key-value pairs, that's why the result is an empty array.
Solution 3: Use 'typeof' operator #
const value = undefined;
if(typeof value !== "undefined" && value !== "null"){
console.log(Object.keys(value));
}
else{
console.log("The arguments are falsy");
}
This is the output:
The arguments are falsy
In the above example, typeof will returns the string text representing the type of the operand's value. The codes within the `if` statement will only be executed if the value variable is not undefined type and not a null object.
Solution 4: Use ternary operator #
Ternary Operator is an alternative way of writing if-else statement. If the expression to the left of the ? is evaluated as true the value at the left of the : is taken. Else, it'll take the value at the right of the :.
const value = undefined;
console.log(value ? Object.keys(value) : Object.keys({}));
This is the output:
[]
Since the value is undefined, it's evaluated as false and Object.keys({}) at the right side of the : is executed. Hence, an empty array is printed.
All the solutions above are pretty simple and easy right?
You shouldn't delete the property of an undefined / a null value. You may use the above solutions to check whether the variable is null or undefined before deleting its existing property.
const value = undefined;
if(value){
delete value.property;
}
The Conclusion #
In this study blog, we've learned what's TypeError: "Cannot convert undefined or null to object" in Javascript. Besides, we'd also understand how it occurs in "Object.keys()", "Object.assign()", and "Object.values()". Lastly, we learned how to solve this error using solutions such as "typeof", "Logical Operator OR (||)", "Ternary Operator", and "if statement".