Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
JavaScript

JavaScript Array shift() Method

  • 01 Sep, 2025
  • Com 2
JavaScript Array shift() Method

JavaScript Array shift() method removes the first element from the array and returns that removed element. It modifies the original array by decreasing its length by one.

Shifting the element in JavaScript

const apps = ["Insta", "Fb", "Snap"];

console.log(apps);

// Output: ["Insta", "Fb", "Snap"]

removed_app = apps.shift();

console.log(removed_app);

// Output: "Insta"

console.log(apps);

// Output: ["Fb", "Snap"]

Syntax

array.shift()

Parameters

None.

An array with a single element

An array with a single element

If you apply a shift() function to an array with one element, it makes the array empty since the first element is the only element.

const single = ['Zaggle'];

const removed = single.shift();

console.log(removed);

// Output: 'Zaggle'

console.log(single);

// Output: []

If you further call the shift() method, it will return undefined, as the input array is now empty.

With a while loop

Let’s clear the whole array with the help of the shift() method using a while loop.

const apps = ["Insta", "Fb", "Snap", "x"];

while ((i = apps.shift()) !== undefined) {
    console.log(apps);
}

// Output:
// [ 'Fb', 'Snap', 'x' ]
// [ 'Snap', 'x' ]
// [ 'x' ]
// []

Until the undefined value is found, it will remove each element of the array.

Empty array

Array.shift() method on an empty array

If the array is empty before the call, it returns undefined.

let apps = [];

removed_app = apps.shift();

console.log(removed_app);

// Output: undefined (since the array is empty)

console.log(apps);

// Output: [] (the array remains empty)

Sparse Arrays

Sparse Arrays using Array.shift() method

If the first element is an empty slot, it will remove that element and return undefined as the hole element from the sparse array.

const sparse_array = [, 'dog', 'cow']; // Index 0 is a empty slot

const removed = sparse_array.shift();

console.log(removed);

// undefined (empty slot treated as undefined)

console.log(sparse_array);

// ['apple', 'banana'] (shifts left, empty slot removed)

That’s all!

Post Views: 16
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

Python Dictionary update() Method
Python min() Function

2 Comments

  1. Post

    April 18, 2019 at 2:27 am

    It works really well for me

    Reply
  2. Alan

    September 4, 2019 at 12:55 pm

    Suits!!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend