The slice() method in JavaScript returns a shallow copy of a portion of an array into a new array, without modifying the original array. It is non-destructive and ideal for copying, extracting subsets, or cloning arrays.

Key Features:

  • Syntax: array.slice(start, end)

    • start (optional): Index to begin extraction (inclusive). Defaults to 0.

    • end (optional): Index to end extraction (exclusive). Defaults to array length.

  • Negative indices count from the end: -1 is the last element, -2 is the second-to-last, etc.

  • Returns: A new array with the selected elements.

Examples:

const fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry'];
console.log(fruits.slice(1, 3));     // ['banana', 'cherry']
console.log(fruits.slice(2));        // ['cherry', 'date', 'elderberry']
console.log(fruits.slice(-2));       // ['date', 'elderberry']
console.log(fruits.slice());         // ['apple', 'banana', 'cherry', 'date', 'elderberry'] (full copy)

Common Use Cases:

  • Cloning arrays: const copy = original.slice();

  • Pagination: Extracting a specific range of items.

  • Immutable updates: Safe for frameworks like React.

  • Working with array-like objects: Convert via Array.prototype.slice.call(obj).

Note: Unlike splice(), slice() does not mutate the original array. It is read-only and preserves the original data.

The second argument to Array.slice() is the upper bound of the slice.

Think of it as array.slice(lowestIndex, highestIndex).

When you slice from index 3 to index 100, there is one item (in your case) that has index >= 3 and < 100, so you get an array with that one item. When you try to take a slice from index 3 to index 0, there can't be any items that meet the conditions index >= 3 and < 0, so you get an empty array.

--EDIT--

Also, array.slice() should never return undefined. That's one of the advantages of using it. If there are no matching values in the array, you just get back an empty array. Even if you say var a = new Array() and don't add any values to it, calling a.slice(0,1) will just give you an empty array back. Slicing from outside of the array bounds will just return an empty array also. a.slice(250) will return [] whereas a[250] will be undefined.

Answer from jshanley on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › slice
Array.prototype.slice() - JavaScript | MDN
The slice() method of Array instances returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.
🌐
W3Schools
w3schools.com › jsref › jsref_slice_array.asp
JavaScript Array slice() Method
The slice() method returns selected elements in a new array.
Discussions

javascript - using .slice method on an array - Stack Overflow
I'm practicing the array section of JavaScript Koan and I'm not fully understanding why these answers are correct. I added my assumptions below if someone could please clarify/let me know if I'm wr... More on stackoverflow.com
🌐 stackoverflow.com
JavaScript Array splice vs slice - Stack Overflow
This will remove one element from the participantForms array at the index position. These are the Javascript native functions, AngularJS has nothing to do with them. ... Can anyone give helpful examples and what would be ideal for each? Like situations that you prefer to use splice or slice? More on stackoverflow.com
🌐 stackoverflow.com
What's the difference between doing [].slice.call(array) and array.slice()? I've seen such []s used many times with the array inside the call. Why? Thanks!
Edit: this as a suppliment to u/dys13 's link: A more modern way of creating an array from a non-array is Array.from() Array.from(arrayLike); https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from Though you may also see spread used a lot since it's pretty terse and works on any iterable (though Array.from isn't dependent on iterables making it more compatible). It's also useful in that it can be combined with other values when creating the new array [...iterable] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax#Spread_in_array_literals More on reddit.com
🌐 r/javascript
35
96
July 3, 2018
javascript - array.slice(-1)[0] -- Can someone explain? - Stack Overflow
A negative index can be used, indicating an offset from the end of the sequence. slice(-2) extracts the last two elements in the sequence. ... A new array containing the extracted elements. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Mimo
mimo.org › glossary › javascript › array-slice
JavaScript Array slice() Method: Syntax, Usage, and Examples
The slice() method returns a shallow copy of a portion of an array into a new array object, selected from a start index to an end index (end not included). The original array remains unchanged. ... Become a full-stack developer. Learn HTML, CSS, JavaScript, and React as well as NodeJS, Express, ...
🌐
Medium
medium.com › swlh › the-javascript-slice-cheat-sheet-3746c9326bbf
The JavaScript Slice Cheat Sheet
November 13, 2020 - ... “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) where start and end represent the index of items in that array…"
🌐
CoderPad
coderpad.io › blog › development › 4-ways-to-use-javascript-slice-method
4 Ways to Use JavaScript .slice() Method and How to Do Each - CoderPad
June 7, 2023 - const arr = ["A", "B", "C", "D", "E", "F"]; const newArr = arr.slice(-3); console.log(newArr);Code language: JavaScript (javascript) Here we only have one index specified: -3. We’ll start from arr[-3], which is D, and we’ll include all elements until the end. So the output, in this case, will be ["D", "E", "F"]. We can use the slice method on a nested array as well:
Find elsewhere
🌐
Codecademy
codecademy.com › docs › javascript › arrays › .slice()
JavaScript | Arrays | .slice() | Codecademy
May 22, 2025 - The .slice() method in JavaScript returns a partial copy of an array, otherwise known as a shallow copy, without altering the original array.
🌐
Medium
windmaomao.medium.com › array-slice-in-javascript-is-super-handy-855237e9538f
Array slice in Javascript is super handy | by Fang Jin | Medium
December 19, 2023 - Javascript isn’t quite flexible when it comes to the parsing and manipulation comparing to other languages such as Python. So I have rely on some comparative ways. If I want to slice the array to take a portion of it, there actually has a built-in function slice for Javascript.
🌐
ReqBin
reqbin.com › code › javascript › kvmd5zzt › javascript-array-slice-example
How do I slice a JavaScript array?
February 8, 2023 - You can also check if it starts ... length of a string. The array.slice(start, end) method returns a shallow copy of the array containing a portion of the original array....
🌐
daily.dev
daily.dev › home › blog › get into tech › js array slice: a beginner's guide
JS Array Slice: A Beginner's Guide
December 22, 2025 - Learn how to use the slice() method in JavaScript to extract parts of an array without altering the original. Discover its syntax, differences from other methods, practical examples, and best practices.
🌐
freeCodeCamp
freecodecamp.org › news › lets-clear-up-the-confusion-around-the-slice-splice-split-methods-in-javascript-8ba3266c29ae
Let’s clear up the confusion around the slice( ), splice( ), & split( ) methods in JavaScript
October 9, 2018 - This usage is valid in JavaScript. An array with different data types: string, numbers, and a boolean. The slice( ) method copies a given part of an array and returns that copied part as a new array.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › TypedArray › slice
TypedArray.prototype.slice() - JavaScript | MDN
The slice() method of TypedArray instances returns a copy of a portion of a typed array into a new typed array object selected from start to end (end not included) where start and end represent the index of items in that typed array. The original typed array will not be modified.
Top answer
1 of 16
433

splice() changes the original array whereas slice() doesn't but both of them returns array object.

See the examples below:

var array=[1,2,3,4,5];
console.log(array.splice(2));

This will return [3,4,5]. The original array is affected resulting in array being [1,2].

var array=[1,2,3,4,5]
console.log(array.slice(2));

This will return [3,4,5]. The original array is NOT affected with resulting in array being [1,2,3,4,5].

Below is simple fiddle which confirms this:

//splice
var array=[1,2,3,4,5];
console.log(array.splice(2));

//slice
var array2=[1,2,3,4,5]
console.log(array2.slice(2));


console.log("----after-----");
console.log(array);
console.log(array2);

2 of 16
130

Splice and Slice both are Javascript Array functions.

Splice vs Slice

  1. The splice() method returns the removed item(s) in an array and slice() method returns the selected element(s) in an array, as a new array object.

  2. The splice() method changes the original array and slice() method doesn’t change the original array.

  3. The splice() method can take n number of arguments and slice() method takes 2 arguments.

Splice with Example

Argument 1: Index, Required. An integer that specifies at what position to add /remove items, Use negative values to specify the position from the end of the array.

Argument 2: Optional. The number of items to be removed. If set to 0(zero), no items will be removed. And if not passed, all item(s) from provided index will be removed.

Argument 3…n: Optional. The new item(s) to be added to the array.

var array=[1,2,3,4,5];
console.log(array.splice(2));
// shows [3, 4, 5], returned removed item(s) as a new array object.
 
console.log(array);
// shows [1, 2], original array altered.
 
var array2=[6,7,8,9,0];
console.log(array2.splice(2,1));
// shows [8]
 
console.log(array2.splice(2,0));
//shows [] , as no item(s) removed.
 
console.log(array2);
// shows [6,7,9,0]

Slice with Example

Argument 1: Required. An integer that specifies where to start the selection (The first element has an index of 0). Use negative numbers to select from the end of an array.

Argument 2: Optional. An integer that specifies where to end the selection but does not include. If omitted, all elements from the start position and to the end of the array will be selected. Use negative numbers to select from the end of an array.

var array=[1,2,3,4,5]
console.log(array.slice(2));
// shows [3, 4, 5], returned selected element(s).
 
console.log(array.slice(-2));
// shows [4, 5], returned selected element(s).
console.log(array);
// shows [1, 2, 3, 4, 5], original array remains intact.
 
var array2=[6,7,8,9,0];
console.log(array2.slice(2,4));
// shows [8, 9]
 
console.log(array2.slice(-2,4));
// shows [9]
 
console.log(array2.slice(-3,-1));
// shows [8, 9]
 
console.log(array2);
// shows [6, 7, 8, 9, 0]

🌐
freeCodeCamp
freecodecamp.org › news › javascript-slice-and-splice-how-to-use-the-slice-and-splice-js-array-methods
How to Use the slice() and splice() JavaScript Array Methods
April 13, 2022 - Unlike the slice() method, the splice() method will change the contents of the original array. The splice() method is used to add or remove elements of an existing array and the return value will be the removed items from the array. If nothing was removed from the array, then the return value will just be an empty array. ... I hope you enjoyed this JavaScript ...
🌐
Built In
builtin.com › software-engineering-perspectives › array-slice-javascript
3 Ways to Use Array Slice in JavaScript | Built In
Summary: Learn three ways to use JavaScript’s slice() method: to copy an array, to get the first N elements in an array and to remove an element at a specific index — all without modifying the original array.
🌐
Reddit
reddit.com › r/javascript › what's the difference between doing [].slice.call(array) and array.slice()? i've seen such []s used many times with the array inside the call. why? thanks!
What's the difference between doing [].slice.call(array) and ...
July 3, 2018 -

EDIT: Thanks for the answers, magnificent people! So Array.from is a better way than [].slice.call, and they're used to provide the array prototype to array-like objects! If done directly, apparently errors are thrown.

Thanks!

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › splice
Array.prototype.splice() - JavaScript | MDN
The splice() method of Array instances changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
Top answer
1 of 3
16

Break the expression down into its parts. By understanding each small piece, you will understand the whole.

This is your original statement: alert(array.slice(-1)[0]);

alert(...) is a function call. Before it can execute (and print something to the screen), its arguments must be evaluated first. It has one argument: array.slice(-1)[0], which we'll examine next.

array.slice(-1) is another function call. [0] is an array index. Which is evaluated first? To answer this, we turn to Operator Precedence. Both function calls and member access are level 19, with left-to-right associativity, which means we evaluate the function call first, then the array index next.

For this, let's turn to the documentation on array.slice, which says:

arr.slice(begin)

A negative index can be used, indicating an offset from the end of the sequence. slice(-2) extracts the last two elements in the sequence.

Return value

A new array containing the extracted elements.

So, array.slice(-1) gives you an array containing the last element from the original array.

Moving left-to-right, we now have an array of a single item followed by an array index [0]. That gives you the first (and only) item from the sliced array, which is then passed to the alert(...).

2 of 3
5

Check out Array.prototype.slice().

When you call array.slice(), that's returning a slice of your array (as another array).

var array = ['zero', 'one', 'two', 'three'];
// Grab the elements from `array`
// beginning at 1 and up to (not including) 3.
var sliced = array.slice(1, 3);

console.log(array);    // ['zero', 'one', 'two', 'three']
console.log(sliced);   // ['one', 'two']
console.log(sliced[0]) // 'one'

In your code, you are executing array.slice(-1). The documentation says that "[a] negative index can be used, indicating an offset from the end of the sequence. slice(-2) extracts the last two elements in the sequence." Thus, your array.slice(-1) is returning a new array populated with the last element of your original, array.

var array = ['zero', 'one', 'two', 'three'];
var sliced = array.slice(-1);

console.log(array);      // ['zero', 'one', 'two', 'three']
console.log(sliced);     // ['three']
console.log(sliced[0]);  // 'three'

// All together, it looks like this. 
// I'm using `alert()` instead of `console.log()` 
// to mirror your code.

alert(array.slice(-1)[0]); // 'three'