The slice()
method in JavaScript is used to extract a portion of an array or a string and return it as a new array or a new string, respectively, without modifying the original array or string.
It’s a non-destructive operation, meaning that the original array or string remains unchanged.
There are multiple usage of the slice()
method:
Working with Arrays
Let’s start by looking at how the slice()
method can be used with arrays.
const fruits = ["apple", "banana", "cherry", "date", "elderberry"];
1. Extracting a portion of an array:
You can use the slice()
method to extract a portion of an array by specifying the starting and ending indices. The start
parameter is the index from where the slice should begin (inclusive), and the end
parameter is the index until where the slice should end (exclusive).
For example:
Extract elements from index 1 (inclusive) to index 3 (exclusive) from fruits
array.
const slicedFruits = fruits.slice(1, 3);
console.log(slicedFruits); // Output: ['banana', 'cherry']
2. Shallow copy of array:
If you omit the start
and end
parameters, slice()
will consider the entire array and create a shallow copy of the original array.
For example:
const copiedFruits = fruits.slice();
console.log(copiedFruits); // Output: ['apple', 'banana', 'cherry', 'date', 'elderberry']
3. Support of negative indices:
slice()
also supports negative indices, which count from the end of the array. -1
represents the last element, -2
represents the second-to-last element, and so on.
const lastTwoFruits = fruits.slice(-2);
console.log(lastTwoFruits); // Output: ['date', 'elderberry']
Notes
It’s important to note that slice()
method does not modify the original array.
Working with Strings
Now let’s see how the slice()
method can be used with strings.
const sentence = "The quick brown fox jumps over the lazy dog";
1. Extracting a portion of a string:
You can use the slice()
method to extract a portion of a string by specifying the starting and ending indices, just like with arrays.
For example:
You can extract a portion of the string like this:
const slicedSentence = sentence.slice(4, 15);
console.log(slicedSentence); // Output: 'quick brown'
2. Create a copy of original string:
If you omit the start
and end
parameters, slice()
method will consider the entire string and create a new string that is a copy of the original string.
For example:
const copiedSentence = sentence.slice();
console.log(copiedSentence); // Output: 'The quick brown fox jumps over the lazy dog'
3. Support Indices:
The slice()
method also supports negative indices with strings, just like with arrays.
For example:
const lastEightChars = sentence.slice(-8);
console.log(lastEightChars); // Output: 'lazy dog'
Notes
It’s important to note that slice()
method does not modify the original string.
The slice()
method in Javascript is a powerful and versatile tool for extracting portions of arrays or strings without modifying the original data. It can be used to create shallow copies of arrays, extract sub-arrays or substrings, retrieve elements from arrays, and handle edge cases.
Thank you for reading, and let’s have conversation with each other
Thank you for reading my article. Let’s have conversation on Twitter and LinkedIn by connecting.