JavaScript Tutorial

JavaScript Array Methods

push, pop, shift, unshift, splice, and concat change or copy a list. Know which ones mutate.

Mutate or copy

Some methods edit the array you already have. Others return a new array and leave the original alone. If you pass a list into a function and it comes back shorter, you mutated it. That is not a bug if you meant to — it is a bug if another part of the page still needed the old items.

Open /javascript/try and log the array before and after each call. Watchlength.

push and pop

push(item) adds at the end and returns the new length. pop() removes the last item and returns it. Both mutate. The end of the list is the cheap end: these two are the usual stack operations.

Example

const tasks = ["write", "review"];
const newLength = tasks.push("ship");
const last = tasks.pop();

console.log(newLength);
console.log(last);
console.log(tasks);

document.body.textContent = tasks.join(", ") + " / popped " + last;

unshift and shift

unshift(item) inserts at index 0. shift() removes index 0 and returns it. Both mutate. Every later item has to move, so these are slower on long lists. Use them when the front of the queue is the point.

Example

const queue = ["second", "third"];
queue.unshift("first");
const served = queue.shift();

console.log(served);
console.log(queue);

document.body.textContent = "served " + served + " / left " + queue.join(", ");

splice edits in the middle

splice(start, deleteCount, ...items) removes deleteCount items atstart and, if you pass extra arguments, inserts them there. It mutates and returns the removed items as an array.

splice(1, 1) deletes one item at index 1. splice(1, 0, "new") inserts without deleting. splice(1, 1, "new") replaces that one slot.

Example

const letters = ["a", "b", "c", "d"];
const removed = letters.splice(1, 2, "X", "Y");

console.log(removed);
console.log(letters);

document.body.textContent =
  "removed " + removed.join(", ") + " / now " + letters.join(", ");

concat and slice copy

concat(other) returns a new array with the items of both lists. Neither original changes.slice(start, end) returns a copy from start up to but not includingend. Omit both arguments and you copy the whole list.

Example

const a = [1, 2];
const b = [3, 4];
const joined = a.concat(b);
const middle = joined.slice(1, 3);

console.log(a);
console.log(joined);
console.log(middle);

document.body.textContent =
  "a stays " + a.join(",") + " / joined " + joined.join(",");

Which methods mutate

MethodMutates?Typical use
push / popYesAdd or take from the end
unshift / shiftYesAdd or take from the front
spliceYesDelete or insert at an index
concatNoJoin two lists into a new one
sliceNoCopy a range
joinNoTurn items into one string

join(separator) builds a string. It does not change the array. That is how the examples print a readable list onto the page.

What to remember

  • End of the list: push and pop.
  • Front of the list: unshift and shift.
  • splice edits in place. concat and slice copy.
  • Log the array after a mutating call before you assume it is unchanged.

Next: walking a list with forEach, map, filter, and find.

FAQ: JavaScript Array Methods

Common questions about this page.

What is the StudyGrid JavaScript tutorial?

The StudyGrid JavaScript tutorial is a full beginner track: variables, functions, arrays, if/else, the DOM, events, storage, and fetch. Each chapter has copy-and-run examples.

Should I run javascript array methods examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn javascript array methods in this JavaScript JavaScript lesson (JavaScript Array Methods).

Is the JavaScript editor the same as Try Python?

No. Try JavaScript is a live page plus a console at /javascript/try. Try Python stays at /try. JavaScript lessons never open the Python editor.

Do I need to install anything to learn JavaScript?

No. Open a chapter, click Try it in JavaScript, and the page and console update in the browser.

Where should I start the JavaScript tutorial?

Start at JavaScript Intro, then Where To, Output, and Syntax. After variables and functions, continue to the DOM and events. Use Next at the bottom of each chapter.

Is the JavaScript tutorial free?

Yes. The JavaScript studio on StudyGrid (studygrid.in) is free: dashboard, chapters, and the live editor.