JavaScript .shift()

BrandonDusch's avatar
Published Jun 18, 2021Updated Jun 9, 2022
Contribute to Docs

Removes and returns the first element of the array. All subsequent elements will shift down one place.

  • Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
    • Includes 34 Courses
    • With Professional Certification
    • Beginner Friendly.
      115 hours
  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      15 hours

Syntax

array.shift();

Example

The following example shows how to shift the daysOfWeeks array and remove the first element:

const daysOfWeek = [
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
'Sunday',
];
const removed = daysOfWeek.shift();
console.log('Removed day:', removed);
console.log('Rest of week:', daysOfWeek);

This will produce the following output:

Removed day: Monday
Rest of week: [ 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' ]

Codebyte Example

The instructions of the alfredoRecipe array are followed by a while loop that prints each step of the recipe to the console:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn JavaScript on Codecademy

  • Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
    • Includes 34 Courses
    • With Professional Certification
    • Beginner Friendly.
      115 hours
  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      15 hours