The Wayback Machine - https://web.archive.org/web/20240830111927/https://www.geeksforgeeks.org/javascript-array-slice-method/
Open In App

JavaScript Array slice() Method

Last Updated : 12 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The Array slice() method returns selected elements in an array as a new array. It selects from a given start, up to a (not inclusive) given end. This method does not change the original array, enabling non-destructive extraction of array segments.

Syntax:

arr.slice(begin, end);

Parameters:

  • begin: This parameter defines the starting index from where the portion is to be extracted. If this argument is missing then the method takes begin as 0 as it is the default start value.
  • end: Parameter specifying the end index for extracting a portion from an array, defaulting to array length if undefined, adjusting for exceeding length.

Return value:

This method returns a new array containing some portion of the original array. 

Example 1: Extracting elements between two indexes

Here, the slice() method extracts the array from the given array starting from index 2 and including all the elements less than index 4.

JavaScript
function func() {
    // Original Array
    let arr = [23, 56, 87, 32, 75, 13];
    // Extracted array
    let new_arr = arr.slice(2, 4);
    console.log(arr);
    console.log(new_arr);
}
func();

Output
[ 23, 56, 87, 32, 75, 13 ]
[ 87, 32 ]

Example 2: Passing no arguments

Here, the slice() method extracts the entire array from the given string and returns it as the answer, Since no arguments were passed to it.

JavaScript
function func() {
    //Original Array
    let arr = [23, 56, 87, 32, 75, 13];
    //Extracted array
    let new_arr = arr.slice();
    console.log(arr);
    console.log(new_arr);
}
func();

Output
[ 23, 56, 87, 32, 75, 13 ]
[ 23, 56, 87, 32, 75, 13 ]

Example 3: Extracting array from index 2

In this example, the slice() method extracts the array starting from index 2 till the end of the array and returns it as the answer.

JavaScript
function func() {

    //Original Array
    let arr = [23, 56, 87, 32, 75, 13];
    
    //Extracted array
    let new_arr = arr.slice(2);
    console.log(arr);
    console.log(new_arr);
}
func();

Output
[ 23, 56, 87, 32, 75, 13 ]
[ 87, 32, 75, 13 ]

Example 4: Slicing the nested Array

In this example, the slice() method extracts the elements from the nested array and returns it as the answer.

JavaScript
function func() {

    // Original Array
    let arr = [23, [87, 32, 75, 27,3,10,18 ,13]];
    
    // Extracted array
    let new_arr = arr[1].slice(2, 4);
    console.log(arr);
    console.log(new_arr);
}
func();

Output
[
  23,
  [
    87, 32, 75, 27,
     3, 10, 18, 13
  ]
]
[ 75, 27 ]

We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.

Supported Browsers

JavaScript Array slice() Method- FAQs

What does the array slice() method do?

The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included). The original array is not modified.

How do you use the slice() method to copy an entire array?

To copy an entire array, call slice() without any arguments.

How can you extract a portion of an array using slice()?

To extract a portion of an array, provide the start and end parameters. The method will return a new array containing elements from the start index up to, but not including, the end index.

What happens if you only provide the start parameter?

If you only provide the start parameter, slice() extracts elements from start to the end of the array.

What happens if you provide negative indices?

Negative indices count back from the end of the array. For example, a start of -3 and an end of -1 will select elements starting three from the end and ending one from the end.

How does slice() handle indices that are out of range?

If start or end is out of range, slice() will treat it as if it were within the bounds of the array. It will extract from the nearest valid index.



Previous Article
Next Article

Similar Reads

Which one method use to delete an element from JavaScript array (slice or delete) ?
We can use both methods to delete an element from a javascript array. The answer is solely dependent on the application and on the programmer. What slice() method does? The slice method, as the name suggests, slices an array and returns a copy of the new array. This method does not affect the main array, it just returns the copy of the array. We ca
4 min read
What is Array.prototype.slice() method in JavaScript ?
The Array.prototype.slice() method in JavaScript is used to extract a portion of an array and create a new array containing the selected elements. It does not modify the original array. instead, it returns a shallow copy of a portion of the array. Syntax:array.slice(startIndex, endIndex);Parameters:startIndex: The index at which to begin extraction
1 min read
What is the difference between Array.slice() and Array.splice() in JavaScript ?
In JavaScript, slice() and splice() are array methods with distinct purposes. `slice()` creates a new array containing selected elements from the original, while `splice()` modifies the original array by adding, removing, or replacing elements. slice():The slice() method in JavaScript extracts a section of an array and returns a new array containin
3 min read
JavaScript arrayBuffer slice() Method
The arrayBuffer.slice is a property in JavaScript that return another arrayBuffer containing the contents of the previous arrayBuffer from beginning inclusive, to end, exclusive in bytes. ArrayBuffer is an object which is used to represent fixed-length binary data. Difference between property and function in javascript. Property in JavaScript is no
2 min read
JavaScript typedArray.slice() Method
The typedArray.slice() is an inbuilt function in JavaScript which is used to return the part of the elements of the given typedArray. typedArray.slice(begin, end) Parameters: It takes two parameter which are specified below- begin: It is the beginning index and it can be negative too.end: It is the ending index and here slice extracts elements up t
2 min read
JavaScript SharedArrayBuffer.prototype.slice() Method
JavaScript SharedArrayBuffer.prototype.slice() method is specifically for the objects of the SharedArrayBuffer type. It is used to create a new SharedArrayBuffer object that references the same memory region as the original but with a different range. Syntax:sharedArrayBufferVariable.slice(start, end);Parameters:start: An optional parameter represe
2 min read
JavaScript String slice() Method
The slice() method in JavaScript is used to extract a portion of a string and create a new string without modifying the original string. Syntax:string.slice(startingIndex, endingIndex);Parameters: This method uses two parameters. This method does not change the original string. startingIndex: It is the start position and it is required(The first ch
3 min read
How does Array.prototype.slice.call work in JavaScript ?
Array.prototype.slice.call()Array.prototype.slice.call() is a method in JavaScript, It is used to convert an array-like or iterable object into a genuine array. It allows you to extract a portion of an array or an array-like object and return a new array. This technique is often used to manipulate and work with non-array objects to convert them int
2 min read
Different Ways to Use Array Slice in JavaScript
In this article, we will see the different ways to use the Array slice method in Javascript. Using Array Slice in JavaScript refers to the technique of extracting a specified portion of an array, defined by start and end indices, to create a new array containing those selected elements. Syntaxarr.slice(begin, end);Parameters: This method accepts tw
3 min read
TypeScript Array slice() Method
The Array.slice() method in TypeScript is a built-in function used to extract a section of an array and return it as a new array. This method does not alter the original array. Syntax: array.slice( begin [,end] ); Parameter: This method accepts two parameters as mentioned above and described below: begin : This parameter is the Zero-based index at
2 min read
Difference between String.slice and String.substring in JavaScript
These 2 functions are quite similar in their Syntax But are different in some cases. Let's see the difference between them. JavaScript slice() Method:This method selects the part of a string and returns the selected part as a new string. Start and end parameters are used to specify the extracted part. The first character starts with index 0. Syntax
3 min read
jQuery slice() Method
The slice() is an inbuilt method in jQuery that is used to select a subset of elements based on its index. The subset is a set that may be a part of a large set. Syntax: $(selector).slice(para1, para2)Parameter: It accepts two parameters which are specified below- para1: It specifies where to start the selection of the elements.para2: It is optiona
1 min read
Ember.js Ember.NativeArray slice() Method
Ember.js is an open-source JavaScript framework used for developing large client-side web applications which is based on Model-View-Controller (MVC) architecture. Ember.js is one of the most widely used front-end application frameworks. It is made to speed up development and increase productivity. Currently, it is utilized by a large number of webs
5 min read
Node.js Buffer.slice() Method
Buffer is a temporary memory storage that stores the data when it is being moved from one place to another, it's like an array of integers. The Buffer.slice() method returns a new Buffer that points to the same memory location as that of the input Buffer with only the cropped elements. Syntax: buffer.slice( start, end ) Parameters: This method acce
2 min read
TypeScript | String slice() Method with example
The slice() is an inbuilt function in TypeScript which is used to extracts a section of a string and returns a new string. Syntax: string.slice( beginslice [, endSlice] ) Parameter: This method accept two parameter as mentioned above and described below: beginSlice - This parameter is the zero-based index at which to begin extraction.endSlice - Thi
1 min read
Underscore.js _.iterators.slice() Method
With the help of _.iterators.slice() method, we can get the value from the iteration function but it will drop the number of values which is given by numberToDrop and return the maximum of numberToTake of the remaining values one by one whenever invoked by using this function. Syntax: _.iterators.slice(iter, numberToDrop, numberToTake) Parameter: T
2 min read
Collect.js slice() Method
The slice() method is used to return a slice of the given collection starting at given index. Syntax: collect(array).slice(size) Parameters: The collect() method takes one argument that is converted into the collection and then slice() method is applied on it. The slice() method holds the size as parameter. Return Value: This method returns a slice
1 min read
What is slice() method in jQuery ?
When there are multiple instances of the selected elements in jQuery, if we want to add particular properties or select elements from a particular index to another index the slice() method in jQuery can be used. Using jQuery's slice we can add properties to only the range of elements we want to add the property. It is a 0 based indexing. Syntax: $(
2 min read
Ember.js MutableArray slice() Method
Ember.js is an open-source JavaScript framework used for developing large client-side web applications which is based on Model-View-Controller (MVC) architecture. Ember.js is one of the most widely used front-end application frameworks. It is made to speed up development and increase productivity. Currently, it is utilized by a large number of webs
5 min read
Ember.js EmberArray slice() Method
Ember.js is an open-source JavaScript framework used for developing large client-side web applications which is based on Model-View-Controller (MVC) architecture. Ember.js is one of the most widely used front-end application frameworks. It is made to speed up development and increase productivity. Currently, it is utilized by a large number of webs
5 min read
Ember.js ArrayProxy slice() Method
Ember.js is an open-source JavaScript framework used for developing large client-side web applications which is based on Model-View-Controller (MVC) architecture. Ember.js is one of the most widely used front-end application frameworks. It is made to speed up development and increase productivity. Currently, it is utilized by a large number of webs
5 min read
Node.js slice() function
The slice() function is a string function of Node.js which is used to extract sub-string from a string. Syntax: string.slice( start, end ) Parameters: This function uses three parameters as mentioned above and described below: string: It holds the string content. The substring is extracted from this string.start: This parameter holds the starting i
1 min read
PHP | Ds\Sequence slice() Function
The Ds\Sequence::slice() function is an inbuilt function in PHP which is used to return the subsequence of given range. Syntax: Ds\Sequence abstract public Ds\Sequence::slice ( int $index [, int $length ] ) Parameters: This function accepts two parameters as mentioned above and described below: $index: This parameter hold the starting index of subs
2 min read
PHP | Ds\Vector slice() Function
The Ds\Vector::slice() function is an inbuilt function in PHP which is used to return the sub-vector of the given vector. Syntax: Ds\Vector public Ds\Vector::slice( $index, $length )/pre> Parameters: This function accepts two parameter as mentioned above and described below: $index: This parameter hold the starting index of sub-vector. The index va
2 min read
PHP Ds\Map slice() Function
The Ds\Map::slice() function of PHP Ds\Map class is used to get a subset of the specified Map instance. The method returns the subset containing elements starting from a specific index till a specified length from the Map instance. Syntax: public Ds\Map::slice(int $index, int $length) Parameter: This function accepts two parameters as described bel
3 min read
PHP | Ds\Deque slice() Function
The Ds\Deque::slice() function is an inbuilt function in PHP which is used to return a sub-Deque which contains elements of the Deque within the index range. Syntax: public Ds\Deque::slice( $index, $length ) : Ds\Deque Parameters: This function accept two parameters as mentioned above and described below: index: This parameter hold the starting ind
2 min read
PHP | Ds\Set slice() Function
The Ds\Set::slice() function is an inbuilt function in PHP which is used to return the sub-set of given range. Syntax: Ds\Set public Ds\Set::slice ( int $index [, int $length ] ) Parameters: This function accepts two parameters as mentioned above and described below: $index: This parameter holds the starting index of sub-set. The index value can be
2 min read
How to slice a string in AngularJS ?
Given a string and the task is to make a slice of the given string using AngularJS. This task can be performed in 2 ways by using the built-in methods in AngularJS, i.e., we can use the slice() method, which is used to return a part or slice of the given input string. The substr() method can also be used to slice a string that returns the specified
3 min read
How to slice image specified by border-image-source in CSS ?
In this article, we will see how to add a border image as a slice image using CSS. To make the border slice image, the border-image-slice property is used. This property divides or slices an image specified by the border-image-source property. The border-image-slice property divides a given image into - 9 regions4 corners4 edgesA middle region The
2 min read
Tensorflow.js tf.slice() Function
Tensorflow.js is an open-source library developed by Google for running machine learning models and deep learning neural networks in the browser or node environment. It also helps the developers to develop ML models in JavaScript language and can use ML directly in the browser or in Node.js. The tf.slice() function is used to extract a slice from t
1 min read