← Back to JavaScript, page 1

javascript-0019 · Easy fresher

What is the difference between slice, splice, and split?

slice returns a part of an array or string without changing the original. splice changes an array in place by removing, replacing, or inserting items, and returns the removed items. split belongs to strings and breaks one string into an array using a separator.

Full answer

The three names look alike but do different work.

slice(start, end) exists on arrays and on strings. It returns a copy of the part from start up to, but not including, end. It never changes the original. Negative numbers count from the end, so slice(-2) returns the last two items.

splice(start, deleteCount, ...itemsToInsert) exists only on arrays and changes the array in place. It removes deleteCount items from start, puts the new items in their place, and returns an array of the removed items. With a delete count of 0 it only inserts.

split(separator, limit) exists only on strings. It cuts the string at every separator and returns an array of the pieces. With an empty string as separator, it splits into UTF-16 code units, which can break emoji, so Array.from(text) or spread is safer for visible characters. The reverse is array.join(separator).

Because splice mutates, it can cause bugs when the array is shared or is state in a UI. Modern JavaScript has non-mutating versions: toSpliced, toSorted, toReversed, and with (ES2023, available in current Node.js and browsers).

javascript
const days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'];

console.log(days.slice(1, 3), days.slice(-2), days);

const removed = days.splice(1, 2);
console.log(removed, days);

days.splice(1, 0, 'Tue', 'Wed');
console.log(days);

const sentence = 'maths,physics,chemistry';
const subjects = sentence.split(',');
console.log(subjects, subjects.join(' | '));
console.log(sentence.slice(0, 5));
console.log('abc'.split(''), sentence.split(',', 2));

const original = [1, 2, 3, 4];
const without = original.toSpliced(1, 2);
console.log(without, original);

Run with Node.js v24.22.1 as js-19.js; two runs gave identical output. Output: [ 'Tue', 'Wed' ] [ 'Thu', 'Fri' ] [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri' ] [ 'Tue', 'Wed' ] [ 'Mon', 'Thu', 'Fri' ] [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri' ] [ 'maths', 'physics', 'chemistry' ] maths | physics | chemistry maths [ 'a', 'b', 'c' ] [ 'maths', 'physics' ] [ 1, 4 ] [ 1, 2, 3, 4 ]

Key points

  • slice: copy a part, no mutation, end index excluded.
  • splice: mutate an array, returns the removed items.
  • split: string to array; join does the reverse.
  • Negative indexes in slice count from the end.
  • toSpliced is the non-mutating form of splice.

Common mistakes

  • Using splice when a copy was intended and changing the original array.
  • Mixing up slice's end index; it is excluded.
  • Expecting splice to return the changed array. It returns the removed items.
  • Splitting text with an empty separator and breaking multi-code-unit characters.

Follow-ups

  • How would you remove one item from an array without mutating it?
  • What does split do with a regular expression that has capture groups?
  • Why is substr discouraged, and what replaces it?

Tips

  • Make a table in your head: mutates or not, array or string, what it returns.
  • Mention toSpliced to show you track the modern language.
  • Use the "end index is excluded" rule in every example; it prevents off-by-one errors in the interview.