JavaScript string.slice() Method
Strings are an essential data type and are present in almost all major programming languages. They have a key role in storage and manipulation of data. JavaScript strings come with several built methods which can be used to manage and manipulate them. The slice() method is one of such methods.
slice() method
The slice() method in JavaScript is used to get a substring from a string by passing the starting and the ending index of the required substring without actually modifying the original string.
Syntax of the slice() method
The slice() method is applied on a string with the help of the dot operator. It requires the name of the string along with the two parameters which are the starting and the ending position of the substring within the original string. It is interesting to note that the second argument i.e., the endingPosition of the substring is totally optional.
Method 1: Using slice() Method by Passing Both Arguments
In the first method, we will use the slice() method by passing both the starting and the ending index.
var subString = originalString.slice(35,40);
console.log(subString);
console.log(originalString);
We first created a variable called originalString to store a string. We then created another variable and used the slice() method to assign it a value. We passed 35 as the starting index and 40 as the ending index of the subString. We then used the console.log() method to show the value of the subString on the console. Lastly, we logged the value of the originalString to the console to show that the original string has remained unchanged.
Method 2: Using slice() Method by Passing a Single Argument
The slice method can also work with only one argument. If a single argument is passed to the slice() method then it takes it as the starting index and the ending index is the end of the string by default:
var subString = originalString.slice(35);
console.log(subString);
Method 3: Using slice() Method by Passing a Negative Value as an Argument
If we pass a negative value to the slice method as an argument then it starts the indexing from the end of the string:
var subString = originalString.slice(-9);
console.log(subString);
We can also pass two negative arguments:
var subString = originalString.slice(-9, -4);
console.log(subString);
Incorrect Arguments:
The slice() method returns an empty string in case the starting index being passed is greater than the ending index of the string:
var subString = originalString.slice(35, 30);
console.log(subString);
The slice() method also returns an empty string in case the starting index being passed is greater than the length of the original string:
Conclusion
The inbuilt str.slice() method is used to get a substring from a string by passing an initial position/index and an optional ending position/index. In this article we have tried every possible use of the slice() method with appropriate examples to see how it behaves with different arguments.
Source: linuxhint.com