unshift()

ES3+

Adds one or more elements to the beginning of an array and returns the new length.

Syntax

array.unshift(element1, element2, ...)

Parameters

elements any

Elements to add to the front of the array

Return Value

number

The new length of the array

Examples

JavaScript
const fruits = ['banana', 'cherry'];
fruits.unshift('apple');
console.log(fruits);
Output:
// ['apple', 'banana', 'cherry']

Related Methods