Array.prototype.unshift()

Overview.

The Array.prototype.unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.

Syntax

array.unshift(element1[, ...[, elementN]])

Parameters

  • element1: The first element to add to the array.
  • elementN (optional): Additional elements to add to the beginning of the array.

return value

The new length of the array after the elements have been added.

Examples

const numbers = [2, 3, 4];
const newLength = numbers.unshift(1);
// numbers is now [1, 2, 3, 4]
// newLength is 4

const numbers2 = [4, 5];
numbers2.unshift(1, 2, 3);
// numbers2 is now [1, 2, 3, 4, 5]

Notes

The unshift() method modifies the original array and returns the new length of the array.

This page was updated on -