Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
JavaScript

JavaScript Array slice() Method

  • 03 Oct, 2025
  • Com 2
JavaScript Array.slice() Method

The array slice() method in JavaScript extracts a shallow copy of a specific portion of an array into a new array object, without modifying the original array. It promotes immutability in functional programming patterns.

Slicing an array in JavaScript

const stocks = ['Netweb', 'Kaynes', 'VMM', 'V2', 'Yatra'];

const sliced_stocks = stocks.slice(1, 3);

console.log(sliced_stocks);

// Output: ['Kaynes', 'VMM']

console.log(stocks)

// Output: [ 'Netweb', 'Kaynes', 'VMM', 'V2', 'Yatra' ]

In this code, we started slicing from index 1 to 3 (excluding). So, the returned array contains only two elements. It is ideal for selecting a range without altering the source.

Syntax

slice(start, end)

Parameters

Argument Description
start (Optional) It represents the zero-based index at which to start extraction from an array.

By default, it is 0 if you omit this argument.

Negative values count backward from the end of the array (e.g., -1 refers to the last element).

end (Optional) It is a number representing the zero-based index at which to end extraction.

By default, it is an array.length if omitted.

Negative values count backward from the end.

Without passing “end” index

When you pass a single argument, it starts at index 2 and goes to the end (stocks.length). Useful for truncating prefixes.

From start to end (omitting end) in slicing an array

const stocks = ['Netweb', 'Kaynes', 'VMM', 'V2', 'Yatra'];

const sliced_stocks = stocks.slice(2);

console.log(sliced_stocks);

// Output: ['VMM', 'V2', 'Yatra']

Negative indices

If you pass a negative index, it will start counting from the end of the array. The last index is -1.

If you pass -2, it resolves to stocks.length – 2 (index 2). So, it will start slicing from negative index 2 up to the end of the list.

Negative indices in array.slice() method

const stocks = ['Netweb', 'Kaynes', 'VMM', 'V2', 'Yatra'];

const negative_sliced_stocks = stocks.slice(-2);

console.log(negative_sliced_stocks);

// Output: [ 'V2', 'Yatra' ] (Index from -2 to end)

In this code, we start extracting elements from -2, meaning the second-to-last element to the last element. And hence, we got the array of the last two elements.

Mixed positive and negative indices

You can pass the positive index “start” as the first argument and the negative index “end” as the second argument.

Passing mixed positive and negative indices to the array.slice() method

const stocks = ['Netweb', 'Kaynes', 'VMM', 'V2', 'Yatra'];

const mixed_sliced_stocks = stocks.slice(1, -2);

console.log(mixed_sliced_stocks);

// Output: [ 'Kaynes', 'VMM' ]

In this code, it starts at index 1, ends at stocks.length – 2 (index 3, excluding ‘V2’ and ‘Yatra’). Combines forward/backward indexing for flexible ranges.

Sparse arrays

The sparse array contains holes, and the slice() method preserves these holes (undefined/empty slots).

The sliced[0] and sliced[2] are empty, maintaining sparsity for consistent iteration.

const sparse_array = ['Netweb', '', 'VMM', '', 'Yatra'];

const sparse_sliced = sparse_array.slice(1, 4);

console.log(sparse_sliced);

// Output: [ '', 'VMM', '' ]

Empty array

If the array is empty, the sliced array will also be empty because there are no elements to slice.

const empty_array = [];

const empty_slice = empty_array.slice(1, 2);

console.log(empty_slice);

// Output: []

Copying the whole array

To create a shallow copy of the array, use the array.slice() method.

const arr = [1, 2, 3];

const copy = arr.slice();

console.log(copy);

// Output: [1, 2, 3]

console.log(copy === arr);

// Output: false (different arrays)

That’s all!

Post Views: 11
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

Python List reverse() Method
How to Reverse a String in Python

2 Comments

  1. vero

    December 18, 2018 at 5:05 am

    I like your tutorials!

    But as someone who is only learning js, i’d like to point out that in this case is good to explain why the result is what it is.
    let namepartner = [‘Pearson’, ‘Specter’, ‘Litt’];
    let suits = namepartner.slice(1, 2);

    console.log(suits);
    Specter

    slice method with parameters 1 and 2 starts at 1 and end at 2(2 not included).
    so as arrays start count as 0, Pearson is ignored, Spencer with position 1 is the result and Litt with position 2 is also ignored.

    also maybe better example would be bigger array.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend