JavaScript String slice() Tutorial

In this section, we will learn what the string slice method is and how to use it in JavaScript.

JavaScript String slice() Method

Sometimes in JavaScript we might encounter a string value and need to extract part of that text and use it in other parts of our program!

This is where we can use the slice() method.

In short, this method is used to extract a sub-string from a text-content and return that sub-string as a result.

Note: this method won’t change the content of the original text-content (string).

JavaScript String slice() Syntax

string.slice(start, end)

String slice() Method Parameters:

The `slice()` method takes 2 arguments:

  • The first argument is the start position (index number) from which the extraction should begin.

Notes:

The first character in a string value starts from the index number 0.

If the first argument is negative, the execution engine will add the length of the string to the negative number and use the result as the value of the first argument.

For example, if the first argument is -2 and the length of that string is 10, then the start position (the first argument) becomes the index number 8.

  • The second argument declares the position (index number) where the extraction should stop.

Notes:

This value is optional and, if ignored, the extraction will start from the start position and end at the last character of the string value itself.

Be aware that this value is excluded! This means if we set the value 5, for example, the extraction will be up to the index number 4 and not 5.

Just like the first argument, if the second argument is a negative number, the engine will add the length of the target string to the negative value and use the result as the value of the second argument.

If, however, the second argument ended up being less than the first argument, then the final return value from this method will be just an empty string!

For example: slice(5,1) will return an empty string.

String slice() Method Return Value:

The return value of this method is a new string that contains the extracted characters from the target text content.

Example: string slice in JavaScript

const id = "If you work hard, then dreams will come true";

const res = id.slice(7, id.length);

const res2 = id.slice(7);

console.log(res);

console.log(res2);

Output:

work hard, then dreams will come true

work hard, then dreams will come true

JavaScript slice vs substring

Both slice and substr methods are used to extract a sub-string from a string value. But there are a couple of differences that you need to know about these two methods:

  1. The second argument in the slice() method is an index number, but in substr() method, the second argument is the number of characters to be extracted!
  2. If we use a negative number as the second argument for the slice() method, it will be added to the length of the target string and then the result will be considered as the index number. But using negative number in substr() method for its second argument will be simply treated as 0 (Basically, there’s no difference between using the value 0 or a negative value as the second argument of the substr() method).
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies