;

Javascript Array - shift() Method


Tutorialsrack 29/10/2022 Jquery Javascript

In this article, you will learn about the javascript Array built-in method Array.prototype.shift(). How does this method work in javascript? 

Array.prototype.shift()

The Array.prototype.shift() method is used to remove the first element from an array and returns that element. 

This method changes the length of the array. If the array is empty it returns undefined

Syntax
array.shift();

The shift() method changes the original array.

Here are some examples of Array.prototype.shift() method:

Examples
//Example 1
const languages = ["JavaScript", "Python", "C++", "Java", "Go"];

// removing the first element from a languages array
const shifted = languages.shift();

console.log("Original Array: ", languages);
/* Output => 

"Original Array: "
["Python", "C++", "Java", "Go"]

*/

// Removed Element from a array
console.log("Removed Element: "+ shifted);
// Output => "Removed Element: JavaScript"

The shift() method has similar behavior to pop(), but the pop() method is applied to the last element in an array.

I hope this article will help you to understand the javascript Array built-in method Array.prototype.shift()

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags