TypeError: slice is not a function in JavaScript [Solved]

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
3 min

banner

# TypeError: slice is not a function in JavaScript

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.

typeerror slice is not a function

Here is an example of how the error occurs.

index.js
const str = 9876; // ⛔️ TypError: slice is not a function const result = str.slice(2);

slice is not a function

We called the slice method on a number so the error occurred. The slice method is implemented by 2 data types:

# Convert the value to the correct type before calling 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.

index.js
// ✅ 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']
The code for this article is available on GitHub

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.

index.js
const num = 9876; const result1 = String(num).slice(1, 3); console.log(result1); // 👉️ "87"

# Check if the value is of the correct type before calling slice

Alternatively, you can conditionally check if the value is of the correct type before calling the slice method.

index.js
// ✅ 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); // 👉️ []
The code for this article is available on GitHub

We used the ternary operator, which is very similar to an if/else statement.

If the expression to the left of the question mark evaluates to a truthy value, the value to the left of the colon is returned, otherwise, the value to the right is returned.

You can also use a simple if statement to achieve the same result.

index.js
// ✅ 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); // 👉️ []
The code for this article is available on GitHub

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.

If the error persists, console.log the value you're calling the slice method on and make sure it's either of type string or array.

# Accessing a property on an object before calling 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.

index.js
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']
The code for this article is available on GitHub

We accessed properties on the object that store a string and an array before calling the slice() method.

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.