🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEjavascript

javascript Documentation

LOADING ENGINE...

Array.slice()

AI & DATA SCIENCE // slice

slice() returns a shallow copy of a portion of an array between start and end indices. The original is NOT modified.

Syntax

array.slice(start, end);
// end is exclusive. Negative indices count from end.

Deep Dive Course

**slice(start, end)** extracts elements from index `start` up to (but not including) `end`. Both are optional: `slice()` copies the whole array. Negative indices count from the end. It performs a **shallow copy** — nested objects are still references.

1Understanding Array.slice()

slice(start, end) extracts elements from index start up to (but not including) end. Both are optional: slice() copies the whole array. Negative indices count from the end. It performs a shallow copy — nested objects are still references.

💡

slice() is the safe way to copy an array: [...arr] and arr.slice() are equivalent. Use slice for extracting sub-arrays without mutation.

editor.html
const arr = ['a', 'b', 'c', 'd', 'e'];

console.log(arr.slice(1, 3));  // ['b', 'c']
console.log(arr.slice(2));     // ['c', 'd', 'e']
console.log(arr.slice(-2));    // ['d', 'e'] (last 2)
console.log(arr.slice());      // full copy
console.log(arr);              // ['a','b','c','d','e'] (unchanged!)
localhost:3000

2Practical Example

Here is a real-world application of Array.slice() showing how it is used in production JavaScript code.

editor.html
// Implement pagination
function paginate(data, page, pageSize) {
  const start = (page - 1) * pageSize;
  return data.slice(start, start + pageSize);
}
const data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
console.log(paginate(data, 2, 3)); // [4, 5, 6]
localhost:3000

3Best Practices

Follow these guidelines when working with Array.slice():

1. Use slice() to safely copy arrays without mutation

2. Use negative indices for 'last N elements': arr.slice(-3)

3. Remember slice(start, end) — end is exclusive

⚠️

Tip: slice() is the safe way to copy an array: [...arr] and arr.slice() are equivalent. Use slice for extracting sub-arrays without mutation.

editor.html
const arr = ['a', 'b', 'c', 'd', 'e'];

console.log(arr.slice(1, 3));  // ['b', 'c']
console.log(arr.slice(2));     // ['c', 'd', 'e']
console.log(arr.slice(-2));    // ['d', 'e'] (last 2)
console.log(arr.slice());      // full copy
console.log(arr);              // ['a','b','c','d','e'] (unchanged!)
localhost:3000

Examples

Example 01Basic Usage
const arr = ['a', 'b', 'c', 'd', 'e'];

console.log(arr.slice(1, 3));  // ['b', 'c']
console.log(arr.slice(2));     // ['c', 'd', 'e']
console.log(arr.slice(-2));    // ['d', 'e'] (last 2)
console.log(arr.slice());      // full copy
console.log(arr);              // ['a','b','c','d','e'] (unchanged!)
Example 02Advanced Example
// Implement pagination
function paginate(data, page, pageSize) {
  const start = (page - 1) * pageSize;
  return data.slice(start, start + pageSize);
}
const data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
console.log(paginate(data, 2, 3)); // [4, 5, 6]

Best Practices

  • Use slice() to safely copy arrays without mutation
  • Use negative indices for 'last N elements': arr.slice(-3)
  • Remember slice(start, end) — end is exclusive

Interview Question

What is a shallow copy and how does slice() produce one?

Hint: Nested objects are still references.

slice() copies element references, not the elements themselves. For primitives (numbers, strings) this is fine — they're immutable. For objects, both the original and copy point to the SAME nested objects. Modifying a nested object in the slice also changes the original. Use structuredClone() or JSON.parse(JSON.stringify()) for deep copies.

Exercises

MediumPractice using Array.slice() in a real scenario.
View Solution
const arr = ['a', 'b', 'c', 'd', 'e'];

console.log(arr.slice(1, 3));  // ['b', 'c']
console.log(arr.slice(2));     // ['c', 'd', 'e']
console.log(arr.slice(-2));    // ['d', 'e'] (last 2)
console.log(arr.slice());      // full copy
console.log(arr);              // ['a','b','c','d','e'] (unchanged!)

Frequently Asked Questions

What is a shallow copy and how does slice() produce one?

slice() copies element references, not the elements themselves. For primitives (numbers, strings) this is fine — they're immutable. For objects, both the original and copy point to the SAME nested objects. Modifying a nested object in the slice also changes the original. Use structuredClone() or JSON.parse(JSON.stringify()) for deep copies.

Related Functions

spliceArray-MethodsElements-Access