Solve - Cannot read property 'name' of Undefined in JS

avatar

Borislav Hadzhiev

Thu Oct 21 20213 min read

Solve - Cannot read property 'name' of Undefined #

The "Cannot read property 'name' of undefined" error occurs when accessing the name property on an undefined value. To solve the error, conditionally check if the value you're accessing the name property on is of the expected type.

cannot read property name of undefined

Here is an example of how the error occurs.

index.js
const obj = undefined; // โ›”๏ธ Cannot read properties of undefined (reading 'name') obj.name;

To solve the error make sure to only access the name property on objects.

index.js
const obj = undefined; // โœ… Using optional chaining const result1 = obj?.name; console.log(result1); // ๐Ÿ‘‰๏ธ undefined // โœ… Using ternary operator const result2 = obj ? obj.name : undefined; console.log(result2); // ๐Ÿ‘‰๏ธ undefined // โœ… Using if/else if (obj) { const result3 = obj.name; } else { console.log('obj is falsy'); }

The first example uses the optional chaining (?.) operator to short-circuit if the reference is equal to undefined or null.

If the value stored in the obj variable is equal to undefined or null, the operator will return undefined, otherwise it will access the name property and return the result.

The second example uses the ternary operator, which is very similar to an if/else statement.

If the value 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.

The last example uses an if/else statement to check if the value stored in the obj variable is truthy.

Truthy are all the values that are not falsy.

The falsy values in JavaScript are: false, undefined, null, 0, "" (empty string), NaN (not a number).

Alternatively, you can provide a fallback value when declaring the object.

index.js
const fromDb = undefined; const obj = fromDb || {};

We used the logical OR (||) operator to provide an empty object as a fallback if the value to the left is falsy (e.g. undefined).

Other Common reasons the "Cannot read property 'name' of undefined" error occurs are:

  1. Calling the method on a class property that is not initialized to a string
  2. Calling the method on an array index that doesn't exist

Here's an example that shows the error being thrown when using arrays.

index.js
const arr = []; // ๐Ÿ‘‡๏ธ Cannot read properties of undefined (reading 'name') arr[0].name();

To solve this, you have to make sure the element at the index is available and an object.

index.js
const arr = []; const result = arr?.[0]?.name || ''; console.log(result); // ๐Ÿ‘‰๏ธ ""

We used the optional chaining operator to short-circuit if either reference is equal to undefined or null and provided an empty string fallback to make sure we return a string from the operation.

If using classes, you have to declare the class property and set it to an empty object before accessing it.

index.js
class Person { // โœ… Initialize from parameters constructor(data) { this.data = data; } getName() { return this.data.name; } } const p1 = new Person({name: 'John Smith'}); console.log(p1.getName()); // ๐Ÿ‘‰๏ธ "John Smith"

We initialized the values for the data class property. Had we not done that, we would get the error when trying to access the property.

Conclusion #

The "Cannot read property 'name' of undefined" error occurs when accessing the name property an undefined value.

To solve the error, make sure to only access the property on objects.

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee