How to use shift and unshift method in javascript

How to use shift and unshift method in javascript

Date: 12/12/2019

Introduction:

In this article, we will explain how to use shift() and unshift() to add and remove the elements from the array in javascript.

shift() used to remove the first element of the array.

unshift() is used to add one or more elements to the array.

shift():

syntax:

arr.shift()

Example:

var arr = [100, 289, 760, 430, 290];
var value = arr.shift();

console.log(value);
console.log(arr);

Output:

  • This function prints the removed element of the array
  • If the array is empty it will return undefined
var arr = [];
var value = arr.shift();

console.log(value);
console.log(arr);

unshift() :

syntax:

arr. unshift(ele1,ele2………)

Example

var arr = [838,90,320,410];
console.log(arr.unshift(220,560));
console.log(arr);

Thanks for using pheonix solutions.

admin

Our team has expertise across software and web development, WordPress, e-commerce, mobile applications, UI/UX, cloud and infrastructure, DevOps, CI/CD, API integration, security, testing, automation, and technical support. The team also works with AI-based software solutions, LLMs, AI workflows, AI agents, and intelligent application development to help businesses automate processes and build smarter digital solutions. We focus on developing, deploying, maintaining, and optimising secure, scalable, and reliable technology solutions while helping businesses adopt modern technologies and drive digital transformation.

Leave a Reply

Scroll to Top