📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Syntax
arr.shift()
JavaScript Array.shift() Method Examples
Example 1: Remove the first element from an array
var array1 = [1, 2, 3];
var firstElement = array1.shift();
console.log(array1);
// expected output: Array [2, 3]
console.log(firstElement);
// expected output: 1
Example 2: Removing an element from an array
var myFish = ['angel', 'clown', 'mandarin', 'surgeon'];
console.log('myFish before:', JSON.stringify(myFish));
var shifted = myFish.shift();
console.log('myFish after:', myFish);
console.log('Removed this element:', shifted);
myFish before: ["angel","clown","mandarin","surgeon"]
myFish after: (3) ["clown", "mandarin", "surgeon"]
Removed this element: angel
Example 3: Using shift() method in while loop
var names = ["Andrew", "Edward", "Paul", "Chris" ,"John"];
while( (i = names.shift()) !== undefined ) {
console.log(i);
}
// Andrew, Edward, Paul, Chris, John
Comments
Post a Comment
Leave Comment