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
Fundamentals
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.
Concept
Understanding the slice() Method
array.slice(start?, end?) returns elements from index start up to but not includingend. 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.
Foundation
📝 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.
Cheat Sheet
⚡ Quick Reference
Goal
Code
Extract range
arr.slice(start, end)
Shallow copy
arr.slice()
From index to end
arr.slice(start)
Last n items
arr.slice(-n)
Mutates array?
No
Compare
📋 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
Hands-On
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.
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.
Compatibility
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 ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
Array.slice()Universal
Bottom line: Safe to use in any environment. No polyfill ever needed.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Array.slice()
Extract a range into a new array safely.
5
Core concepts
📝01
Syntax
slice(s, e)
API
📋02
New array
Extracted.
Output
e03
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.