Last updated: Mar 3, 2024
Reading time·3 min
The "TypeError: slice is not a function" error occurs when the slice()
method is called on a value that is not of type string or array.
To solve the error, convert the value to a string or array before calling the
method or make sure to only call the slice
method on strings or arrays.
Here is an example of how the error occurs.
const str = 9876; // ⛔️ TypError: slice is not a function const result = str.slice(2);
We called the slice
method on a number so the error occurred. The slice
method is implemented by 2 data types:
slice()
To solve the error, either convert the value to a string or an array before calling the method, or only call the method if the value is of the correct type.
// ✅ convert to String before calling slice() const num = 9876; const result1 = num.toString().slice(2); console.log(result1); // 👉️ "76" // ✅ convert to array before calling slice() const set = new Set(['a', 'b', 'c']); const result2 = Array.from(set).slice(2); console.log(result2); // 👉️ ['c']
We used the toString()
method to convert the number to a string before calling
the String.slice()
method.
In the second example, we used the
Array.from() method to convert a Set
to an array before calling the Array.slice()
method.
You can also use the String()
constructor to convert a value to a string.
const num = 9876; const result1 = String(num).slice(1, 3); console.log(result1); // 👉️ "87"
slice
Alternatively, you can conditionally check if the value is of the correct type
before calling the slice
method.
// ✅ check if STRING before calling slice() const num = 9876; const result1 = typeof num === 'string' ? num.slice(2) : ''; console.log(result1); // 👉️ "" // ✅ check if ARRAY before calling slice() const set = new Set(['a', 'b', 'c']); const result2 = Array.isArray(set) ? set.slice(2) : []; console.log(result2); // 👉️ []
We used the ternary operator, which is very similar to an if/else
statement.
You can also use a simple if
statement to achieve the same result.
// ✅ check if STRING before calling slice() const num = 9876; let result1 = ''; if (typeof num === 'string') { result1 = num.slice(2); } else { console.log('The value is not a string'); } console.log(result1); // ----------------------------------------------- // ✅ check if ARRAY before calling slice() const set = new Set(['a', 'b', 'c']); let result2 = []; if (Array.isArray(set)) { result2 = set.slice(2); } else { console.log('The value is not an array'); } console.log(result2); // 👉️ []
In the first example, we check if the value has a type of string
. If it does,
we return the result of calling the slice
, otherwise, we return an empty
string.
In the second example, we check if the value is an array using the Array.isArray() method.
If the value is an array, we return the result of calling the slice
method,
otherwise, we return an empty array.
console.log
the value you're calling the slice
method on and make sure it's either of type string or array.slice()
If you have an object, there is a very good chance you have to access a specific property on the object that has a value of type string or array.
const obj = { name: 'bobbyhadz.com', letters: ['a', 'b', 'c', 'd'], }; // ✅ access string property const result1 = obj.name.slice(0, 5); console.log(result1); // 👉️ 'bobby' // ✅ access array property const result2 = obj.letters.slice(2); console.log(result2); // 👉️ ['c', 'd']
We accessed properties on the object that store a string and an array before
calling the slice()
method.