JavaScript Array slice() Method

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Non-mutating

What You’ll Learn

The slice() method extracts a section of an array into a new array without changing the original. This tutorial covers start and exclusive end, negative indices, shallow copies, comparison with splice(), and five hands-on examples.

01

Syntax

slice(s, e)

02

New array

Extracted

03

end

Exclusive

04

Negative

From end

05

Shallow

Copy refs

06

ES3

Universal

Introduction

When you need part of a list—the middle items, everything after the first, or a paginated chunk—slice() copies that range into a fresh array. The source list stays untouched, which makes it ideal before sorting, reversing, or passing data to functions that should not mutate shared state.

Think of slice() as scissors that cut out a segment and paste it onto a new page, leaving the original page intact.

Understanding the slice() Method

array.slice(start?, end?) returns elements from index start up to but not including end. Omitting both arguments copies the entire array (shallow copy).

Negative indices count from the end: -1 is the last element, -2 is second-to-last. Both parameters are optional and clamped to valid bounds.

💡
Beginner Tip

Do not confuse slice() (copy a range) with splice() (remove/insert and mutate). The names look alike, but behavior is opposite for immutability.

📝 Syntax

General form of Array.prototype.slice:

JavaScript
array.slice(start, end)

Parameters

  • start — zero-based index to begin (default 0). Negative counts from end.
  • end — zero-based index to stop before (default length). Exclusive. Negative counts from end.

Return value

  • New array with selected elements (may be empty).
  • Original array unchanged.

Common patterns

  • arr.slice(1, 4) — indices 1, 2, 3.
  • arr.slice() — shallow copy of all elements.
  • arr.slice(2) — from index 2 to end.
  • arr.slice(-2) — last two elements.
  • arr.slice(0, -1) — all except last.

⚡ Quick Reference

GoalCode
Extract rangearr.slice(start, end)
Shallow copyarr.slice()
From index to endarr.slice(start)
Last n itemsarr.slice(-n)
Mutates array?No

📋 slice() vs splice() vs spread [...] vs substring()

slice copies array ranges; splice mutates; spread copies arrays; substring is for strings.

slice
copy range

Arrays only

splice
remove/insert

Mutates

spread
[...arr]

Shallow copy

substring
strings

Not arrays

Examples Gallery

Open DevTools Console (F12) or use Try-it labs. Example N maps to ?tryit=N.

📚 Getting Started

Extract a middle section with start and end.

Example 1 — Extract a Range (End Is Exclusive)

Take fruits from index 1 up to (not including) index 4.

JavaScript
const fruits = ["apple", "orange", "banana", "grape", "kiwi"];

const middle = fruits.slice(1, 4);

console.log(middle);
// ["orange", "banana", "grape"]
Try It Yourself

How It Works

Indices included: 1, 2, 3. Index 4 ("kiwi") is excluded because end is exclusive.

Example 2 — Shallow Copy with slice()

No arguments copies every element into a new array.

JavaScript
const fruits = ["apple", "orange", "banana"];

const copy = fruits.slice();

console.log(copy);
// ["apple", "orange", "banana"]

console.log(copy === fruits);
// false (different array object)
Try It Yourself

How It Works

Shallow copy: nested objects inside are still shared. For deep clones, use structured cloning or a library.

📈 Practical Patterns

Negative indices, immutability, and splice contrast.

Example 3 — Negative Index: Last Two Elements

slice(-2) starts two positions from the end.

JavaScript
const nums = [10, 20, 30, 40, 50];

console.log(nums.slice(-2));
// [40, 50]

console.log(nums.slice(0, -1));
// [10, 20, 30, 40] — all but last
Try It Yourself

How It Works

Negative start counts backward from length. Negative end also counts from the end.

Example 4 — Original Array Stays the Same

Assign slice results without affecting the source.

JavaScript
const colors = ["red", "green", "blue", "yellow"];

const subset = colors.slice(1, 3);

console.log("subset:", subset);
// ["green", "blue"]

console.log("original:", colors);
// ["red", "green", "blue", "yellow"]
Try It Yourself

How It Works

Safe to slice before passing data to UI components or functions that should not mutate props.

Example 5 — slice() Copies; splice() Removes

See immutability vs mutation side by side.

JavaScript
const forSlice = ["a", "b", "c", "d"];
const forSplice = ["a", "b", "c", "d"];

const copied = forSlice.slice(1, 3);
console.log("slice copy:", copied);
// ["b", "c"]

console.log("after slice:", forSlice);
// ["a", "b", "c", "d"] — unchanged

const removed = forSplice.splice(1, 2);
console.log("splice removed:", removed);
// ["b", "c"]

console.log("after splice:", forSplice);
// ["a", "d"]
Try It Yourself

How It Works

slice for read-only extraction. splice when you intentionally delete or insert in place.

🚀 Common Use Cases

  • Paginationitems.slice(page * size, (page + 1) * size).
  • Clone before mutate — copy then sort/reverse safely.
  • Drop first/lastslice(1) or slice(0, -1).
  • Preview subset — show first N items in UI.
  • Immutable updates — replace a range in React state via slice + spread.
  • Arguments object — legacy: Array.prototype.slice.call(arguments).

🧠 How slice() Runs

1

Resolve start/end

Apply defaults and negative index math.

Bounds
2

Allocate new array

Size = end - start (may be zero).

Create
3

Copy elements

Shallow copy from start to end - 1.

Extract
4

Return new array

Source array untouched.

📝 Notes

  • Does not mutate the original array.
  • end is exclusive (not included).
  • Shallow copy—nested objects are shared references.
  • slice() with no args copies the whole array.
  • Not the same as string slice() or substring().
  • Empty range returns [], not an error.
  • ES3—supported everywhere JavaScript runs.

Browser & Runtime Support

Array.prototype.slice() has been available since JavaScript 1.2 / ES3.

Baseline · ES3

Array.prototype.slice()

Supported in every browser ever shipped with JavaScript arrays, including Internet Explorer 3+, and all Node.js versions.

100% Universal support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
Array.slice() Universal

Bottom line: Safe to use in any environment. No polyfill ever needed.

Conclusion

slice() extracts portions into a new array while leaving the source unchanged. Master exclusive end, negative indices, and shallow copies—and keep it separate from mutating splice().

Next, learn some() to test whether any element passes a condition.

💡 Best Practices

✅ Do

  • Use when you need a copy or subset without mutation
  • Remember end is exclusive
  • Use negative indices for “last n” patterns
  • Clone with slice() before in-place sorts
  • Prefer spread [...arr] for full shallow copies too

❌ Don’t

  • Confuse slice with splice
  • Expect deep clones from slice()
  • Include end index in your mental model
  • Use slice on strings (use string methods instead)
  • Assume slice mutates like pop or shift

Key Takeaways

Knowledge Unlocked

Five things to remember about Array.slice()

Extract a range into a new array safely.

5
Core concepts
📋 02

New array

Extracted.

Output
e 03

Exclusive

end omitted.

Bounds
04

Negative

From end.

Index
05

vs splice

Copy vs cut.

Compare

❓ Frequently Asked Questions

slice() returns a new array containing elements from start index up to (but not including) end index. The original array is not changed.
No. slice() always returns a new array. The source array keeps the same elements and order.
No. end is exclusive. slice(1, 4) includes indices 1, 2, and 3—not index 4.
Negative start or end counts from the end of the array. slice(-2) takes the last two elements. slice(0, -1) takes all but the last.
slice() extracts without mutating. splice() removes or inserts elements in the original array and returns removed items.
slice() has been available since JavaScript 1.2 / ES3. It works in every browser including very old environments.
Did you know?

Before spread syntax, arr.slice() was the idiomatic way to clone an array. Today [...arr] is equally common, but slice still shines when you need partial ranges.

Continue to some()

Learn how to test if at least one array element matches a condition.

some() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful