Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

Shifting Elements

Shifting is akin to popping, but it operates on the first element instead of the last.

JavaScript Array shift()

The shift() method eliminates the first element of the array and shifts all subsequent elements to a lower index.

Example

const fruits = [“Banana”“Orange”“Apple”“Mango”];
fruits.shift();

The shift() method returns the value that was removed from the array’s beginning.

Example

const fruits = [“Banana”“Orange”“Apple”“Mango”];
let fruit = fruits.shift();

JavaScript Array unshift()

The unshift() method inserts a new element at the beginning of an array and shifts existing elements to higher indices.

Example

const fruits = [“Banana”“Orange”“Apple”“Mango”];
fruits.unshift(“Lemon”);

The unshift() method returns the updated length of the array.

Example

const fruits = [“Banana”“Orange”“Apple”“Mango”];
fruits.unshift(“Lemon”);