JavaScript Array slice() method30 Jul 2025 | 5 min read ![]() The JavaScript array slice() method extracts a part of the given array and returns it. This method doesn't change the original array. SyntaxThe slice() method is represented by the following syntax: Parameterstart - It is optional. It represents the index from where the method starts to extract the elements. end - It is optional. It represents the index at where the method stops extracting elements. The slice() method returns a new array that contains the extracted elements. Examples of JavaScript Array slice() MethodLet us understand array slice() method with the help of examples. Example1: Array slice() method without a parameterIn this example, we will slice an array without passing any parameters. Code: ExampleExecute NowOutput: [ 'Apple', 'Banana', 'Orange', 'Mango' ] Explanation: In the upper code, we have created a variable named myArray using let. This array contains fruit names. We utilized the slice() method on the myArray and stored the result in the variable res then we logged the result to the console. As we have called the slice() method without any parameters that is why it will copy the entire array as it is. Example 2: Array slice() method with an index numberIn this example, we will slice an array by passing only an index number which will be an index number. It will return the elements from the array from that index. Code: ExampleExecute NowOutput: [ 'Mango' ] Explanation: In the upper code, we defined myArray which consists of four string elements. We used the slice() method with one parameter on the myArray and stored the result in the variable res then logged the result. We have given 3 as a parameter which is an index value that slices the array starting from index 3 up to the end of the array. Example 3: Array slice() method with two index numbersIn this example, we will slice an array by passing two index numbers. It will return all the elements between those two indices. It will include the value of the first parameter and exclude the value of the second parameter. Code: ExampleExecute NowOutput: [ 'Banana', 'Orange' ] Explanation: In the upper example, we defined a variable named myArray consisting of string elements. We used the slice() method with two parameters on myArray. We stored the result in the res and got output to the console. It will slice the array starting at index number 1 and end the array at index number 3. Example 4: Passing a single negative index value.In the example below, we will give a single negative value as an index to extract an element. Code: ExampleExecute NowOutput: [ 'Bootstrap' ] Explanation: In the above code, we defined an array named arr consisting of four elements. We have passed a negative index value in the slice() method and stored the output in the variable named result. A negative index means counting the elements from the end of the array so we have entered -1 index which gives the last value of the array. Example 5: Passing two negative index values.In this example, we will give two negative index values to extract elements from the given array. Code: ExampleExecute NowOutput: [ 'AngularJS', 'Node.js', 'JQuery' ] Explanation: In the upper code, we defined an array named arr containing four elements. We have passed two negative indices and stored the output in the variable named result. We have passed -4 and -1 values. The index -4 corresponds to the first element and -1 corresponds to the last element. So slice(-4, -1) extracts the last elements from the array that come between -4 and -1. Uses of slice() method:
Conclusion:The slice() method method in an array is one of the most important methods for extracting a part of an array. In this article, we have understood what the slice() method is and its various ways of extracting some portion of an array and the uses of the slice method. Frequently Asked Questions (FAQs)1. What does the slice() method in an array do? The slice() method in an array extracts a part of an array and constructs a new array with the part that was extracted. When a developer utilized the slice() method, it does not alter the original array. 2. How does slice() differ from splice()? The slice() method does not make any changes in the original array. It returns a new array. However, the splice() method makes changes in the original array by adding, replacing or removing elements. 3. How can slice() be utilized to extract the last element of an array or a string? We can utilize a negative index value which can count values from the end. For example, myArray.slice(-1) returns an array with the last element. 4. Can we use the slice() method for strings as well as arrays? Yes, the slice() method can be utilized on both strings & arrays in JavaScript. This method itself acts differently for the type of data on which it is performed and returns a value accordingly. For strings, slice() extracts characters and returns a new string, while for arrays, slice() extracts elements and returns a new array. 5. Does the slice() method makes a deep copy or a shallow copy? The slice() method makes a shallow copy. The primitive data types like numbers, strings, booleans, etc., are copied by their value, but objects and nested arrays are copied by reference. If we modify a nested object in the copied array, that modification will reflect in the original array since both arrays are referring to the same object in memory. Next TopicJavaScript Array |
We request you to subscribe our newsletter for upcoming updates.