Cannot read property 'toLowerCase' of Undefined in JS

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

Cannot read property 'toLowerCase' of Undefined in JS #

The "Cannot read property 'toLowerCase' of undefined" error occurs when calling the toLowerCase() method on an undefined value. To solve the error, initialize the value to an empty string or make sure to only call the toLowerCase method on strings.

cannot read property tolowercase of undefined

Here is an example of how the error occurs.

index.js
const str = undefined; // ⛔️ TypeError: Cannot read properties of undefined (reading 'toLowerCase') str.toLowerCase();

To solve the error, initialize the value of the variable to an empty string, or make sure to only call the String.toLowerCase method on strings.

index.js
const someVar = undefined; // ✅ Initialize to empty string const str = someVar || ''; // 👉️ "" // ✅ Using ternary const result1 = typeof str === 'string' ? str.toLowerCase() : ''; console.log(result1); // 👉️ "" // ✅ Using optional chaining const result2 = str?.toLowerCase() || ''; console.log(result2); // 👉️ "" // ✅ Using if/else if (typeof str === 'string') { const result3 = str.toLowerCase(); } else { console.log('str is not a string'); } // ✅ Initialize to empty string const result4 = (str || '').toLowerCase(); console.log(result4); // 👉️ ""

When declaring the str variable we used the logical OR (||) operator to provide a fallback in case someVar stores a falsy value (e.g. undefined).

index.js
const someVar = undefined; const str = someVar || ''; // 👉️ ""

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

index.js
const someVar = undefined; const result1 = typeof str === 'string' ? str.toLowerCase() : ''; console.log(result1); // 👉️ ""
If the expression to the left of the question mark is falsy (e.g. undefined), the value to the left of the colon is returned, otherwise the value to the right of the colon is returned.

The next example uses the optional chaining (?.) operator. The operator will short-circuit instead of throwing an error if the value to the left is equal to null or undefined.

index.js
const someVar = undefined; const result2 = str?.toLowerCase() || ''; console.log(result2); // 👉️ ""

The next example uses a simple if/else statement where we check if the value the str variable stores is a string before calling the toLowerCase() method.

index.js
const someVar = undefined; if (typeof str === 'string') { const result3 = str.toLowerCase(); } else { console.log('str is not a string'); }

The last example uses the logical OR (||) operator to provide a fallback if the value is falsy.

index.js
const someVar = undefined; const result4 = (str || '').toLowerCase(); console.log(result4); // 👉️ ""

Common reasons the "Cannot read property 'toLowerCase' 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 'toLowerCase') arr[0].toLowerCase();

To solve this, you have to make sure the element at the index is available and of type string.

index.js
const arr = []; const result = typeof arr?.[0] === 'string' ? arr[0].toLowerCase() : ''; console.log(result); // 👉️ ""

Before calling the toLowerCase method, we check if the array element at the specific index is a string.

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

index.js
class Person { // ✅ Initialize to empty string last = ''; // ✅ Initialize from parameters constructor(first) { this.first = first; } lowerFirst() { return this.first.toLowerCase(); } lowerLast() { return this.last.toLowerCase(); } } const p1 = new Person('John'); p1.lowerFirst(); p1.lowerLast();

We initialized the values of the first and last class properties. Had we not done that, we would get the error when trying to access the properties.

Conclusion #

The "Cannot read property 'toLowerCase' of undefined" error occurs when calling the toLowerCase() on an undefined value.

To solve the error, make sure to only call the toLowerCase method on strings.

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.