Cannot read properties of undefined (reading 'slice') in JS

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
3 min

banner

# Cannot read properties of undefined (reading 'slice') in JS

The "TypeError: Cannot read properties of undefined (reading 'slice')" error occurs when the slice() method is called on an undefined value.

To solve the error, make sure to only call the slice method on data types that implement it - arrays or strings.

cannot read property slice of undefined

Here is an example of how the error occurs.

index.js
const arr = undefined; // โ›”๏ธ TypeError: Cannot read properties of undefined (reading 'slice') arr.slice(0);
To solve the error, provide a fallback value if the variable stores a falsy value, or conditionally check if the value is of the correct type.

# Specify a fallback value if the variable stores undefined

Here are examples of how to solve the error when using the slice() method with arrays and strings.

index.js
// ๐Ÿ‘‡๏ธ using slice() with ARRAYS const fromDb = undefined; // โœ… Provide empty array fallback value const arr = fromDb || []; // โœ… Using optional chaining const r1 = arr?.slice(0); // โœ… Using the ternary operator const r2 = arr ? arr.slice(0) : []; // โœ… Using Array.isArray if (Array.isArray(arr)) { const r3 = arr.slice(0); } else { console.log('arr is not an array'); } // โœ… Provide fallback in place const r4 = (arr || []).slice(0);

specify fallback value if the variable stores undefined

The code for this article is available on GitHub

Here are the same examples, applied to using the slice method with strings.

index.js
// ๐Ÿ‘‡๏ธ๏ธ๏ธ using slice() with STRINGS const fromDb = undefined; const str = fromDb || ''; // โœ… Using optional chaining const r5 = str?.slice(0); // โœ… Using the ternary operator const r6 = str ? str.slice(0) : ''; // โœ… Using the typeof operator if (typeof str === 'string') { const r7 = str.slice(0); } else { console.log('str is not a string'); } // โœ… Provide fallback in place const r8 = (str || '').slice(0);

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

index.js
const fromDb = undefined; // โœ… with Arrays const arr = fromDb || []; console.log(arr); // ๐Ÿ‘‰๏ธ [] // -------------------------------- // โœ… with Strings const str = fromDb || ''; console.log(str); // ๐Ÿ‘‰๏ธ ""
The code for this article is available on GitHub

The logical OR (||) operator returns the value to the right if the value to the left is falsy.

The next example shows how to use the optional chaining (?.) operator, which enables us to short-circuit if the reference is equal to undefined or null.

index.js
const fromDb = undefined; // โœ… with Arrays const arr = fromDb || []; const r1 = arr?.slice(0); console.log(r1); // ๐Ÿ‘‰๏ธ [] // -------------------------------- // โœ… with Strings const str = fromDb || ''; const r5 = str?.slice(0); console.log(r5); // ๐Ÿ‘‰๏ธ ""

You can also use the ternary operator which is very similar to an if/else statement.

index.js
const fromDb = undefined; // โœ… with Arrays const arr = fromDb || []; const r2 = arr ? arr.slice(0) : []; console.log(r2); // ๐Ÿ‘‰๏ธ [] // -------------------------------- // โœ… with Strings const str = fromDb || ''; const r6 = str ? str.slice(0) : ''; console.log(r6); // ๐Ÿ‘‰๏ธ ""
The code for this article is available on GitHub
If the value to the left of the question mark is truthy, the ternary operator returns the value to the left of the colon, otherwise, the value after the colon is returned.

You can also check if the value you're calling the slice() method on is of the correct type by using the Array.isArray() method or the typeof operator.

index.js
const fromDb = undefined; // โœ… with Arrays const arr = fromDb || []; if (Array.isArray(arr)) { const r3 = arr.slice(0); } else { console.log('arr is not an array'); } // -------------------------------- // โœ… with Strings const str = fromDb || ''; if (typeof str === 'string') { const r7 = str.slice(0); } else { console.log('str is not a string'); }
The code for this article is available on GitHub

The Array.isArray() method returns true if the provided value is an array, otherwise, false is returned.

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