JavaScript Basics

JavaScript Advanced

JavaScript Arrays

JavaScript Functions

JavaScript Objects

JavaScript DOM

JavaScript String

How to extract a part of a string in JavaScript?

If you want to extract a part of a string, then use the slice() method of the String.

The syntax for the slice() method is:

let subString = originalString.slice(startIndex, endIndex);

The slice() method accepts two arguments, startIndex and endIndex.

  • The startIndex points to the index position of the first character that will be included in the substring.
  • The endIndex is not included in the substring; instead, the endIndex - 1 index position is included.

Note: The slice() method does not modify the original string.

The following example explains you how to use the slice() method in JavaScript:

let str = "JavaScript String";
console.log(str.slice(4, 10)); //Script

Depending on the values passed to the startIndex and the endIndex, you will face the following situations:

1. If startIndex is greater than or equal to the endIndex, then the slice() method returns an empty string.

let str = "JavaScript String";

console.log(str.slice(8,3)); //""

The following image shows the indexing of each letter present in the string "JavaScript String" This image will help you visualize the working of the slice() method.

JavaScript String slice() method

2. If endIndex is not passed, then the slice() method will return the substring that goes from the startIndex till the end of the string.

let str = "JavaScript String";

console.log(str.slice(11)); //"String"

3. If startIndex is negative, then the startIndex is internally converted to its positive index value and compared with the endIndex. If the startIndex after conversion is greater than or equal to the endIndex, then an empty string is returned; otherwise, a substring is returned.

let str = "JavaScript String";

console.log(str.slice(-6, 9)); //""
console.log(str.slice(-13, 10)); //"Script"

In this code, you can see that str.slice(-6, 9) returns an empty string because when -6 is converted to its positive index value, it gives 11. 11 is greater than 9, so an empty string is returned.

On the other hand, in str.slice(-13, 10), when -13 is converted to its positive index value, it gives 4. 4 is smaller than 10, so a substring Script is returned.

4. Similarly, if endIndex is negative, then the endIndex is internally converted to its positive index value and compared with the startIndex. If the endIndex after conversion is less than or equal to the startIndex, then an empty string is returned; otherwise, a substring is returned.

let str = "JavaScript String";

console.log(str.slice(4, -7)); //"Script"
console.log(str.slice(11, -13)); //""

In this code, you can see that str.slice(4, -7) returns a substring Script because when -7 is converted to its positive index value, it gives 10. So 10 is greater than 4; that is why a substring is returned.

In another line of code, str.slice(11, -13), an empty string is returned because the positive index value of -13 is 4. 4 is smaller than 11, so an empty string is returned.

Recommended Posts