Mastering the Array shift() and Array unshift() Methods in JavaScript

Do you want to know more about the shift() and unshift() methods in JavaScript arrays?

In this article, you’ll learn how to use these methods to manipulate arrays and understand their differences.

With clear explanations, practical examples, and detailed code snippets, this article will help you master the array.shift() and array.unshift() methods in JavaScript.

So what are you waiting for? Read on and start mastering these powerful array methods today!

What is shift method in JavaScript?

The shift() method is a built-in JavaScript function that removes the first element from an array and returns that removed element. This method changes the length of the array.

The shift() method is a mutating method, meaning it changes the original array. If an array is empty, calling the shift() method on it will return undefined.

Syntax

 array.shift() ✅

Parameters

None

Return value

This method returns the removed element from the array and it will return undefined if the array is empty.

Here’s an example of how to use the shift() method:

let subjects = ["English", "Math", "Programming", "Web Development", "History"];
console.log("subjects before:", subjects);


const shifted = subjects.shift(); ✅
console.log("subjects after:", subjects);

console.log("Removed this element:", shifted);

Output:

subjects before: [ 'English', 'Math', 'Programming', 'Web Development', 'History' ]

subjects after: [ 'Math', 'Programming', 'Web Development', 'History' ]

Removed this element: English

Now, let’s proceed to array unshift in JavaScript.

What is array unshift in JavaScript?

The array.unshift() method is a built-in JavaScript function that adds one or more elements to the beginning of an array and returns the new length of the array.

This method changes the length of the array. The unshift() method is a mutating method, meaning it changes the original array.

Syntax

array.unshift() 

array.unshift(element0) 

array.unshift(element0, element1) 

array.unshift(element0, element1, …, elementN) ✅

Parameters

elemen01, element1, …, elementN 

The elements to add to the front of the array.

Return value

The new length property of the object upon which the method was called.

Here’s an example of how to use the unshift() method:

let subjects = ["English", "Math", "Programming", "Web Development", "History"];
console.log("subjects before:", subjects);

const unshifted = subjects.unshift("Software Development"); ✅
console.log("subjects after:", subjects);

console.log("New length:", unshifted);

Output:

subjects before: [ 'English', 'Math', 'Programming', 'Web Development', 'History' ]

subjects after: [
  'Software Development',
  'English',
  'Math',
  'Programming',
  'Web Development',
  'History'
]

New length: 6

Example usage of Unshift method in JavaScript

Here are some example codes that use the unshift() method in different scenarios:

Adding multiple elements to the beginning of an array

let subjects = ["English", "Math", "Programming", "Web Development", "History"];
subjects.unshift("Science", "Software Development"); ✅
console.log(subjects); 

Output:

[
  'Science',
  'Software Development',
  'English',
  'Math',
  'Programming',
  'Web Development',
  'History'
]

Using unshift() in a function to add elements to an array

function addSubjects(arr, ...elements) {
    arr.unshift(...elements); 
}

let subjects = ["English", "Math", "Programming", "Web Development", "History"];
addSubjects(subjects, "Software Engineering", "Software Development");
console.log(subjects); 

Using unshift() to add elements to an array in a loop

let subjects = ["English", "Math", "Programming", "Web Development", "History"];
const newSubjects = ["Software Development", "Software Engineering"];
for (let i = newSubjects.length - 1; i >= 0; i--) {
subjects.unshift(newSubjects[i]);
}
console.log(subjects);

Output:

[
  'Software Development',
  'Software Engineering',
  'English',
  'Math',
  'Programming',
  'Web Development',
  'History'
]

Difference between shift and unshift in JavaScript

The shift() and unshift() methods are both built-in JavaScript functions that can be used to manipulate the elements of an array.

The main difference between the two methods is that the shift() method removes the first element from an array and returns that removed element.

On the other hand, the unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.

Conclusion

In conclusion, the shift() and unshift() methods are powerful tools for manipulating arrays in JavaScript

The shift() method removes the first element from an array and returns that removed element, while the unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.

With clear explanations, practical examples, and detailed code snippets, this article has provided a comprehensive guide to mastering these methods.

We hope that you have found this article helpful and informative and that you will be able to use the array shift and array unshift in JavaScript effectively.

If you want to dive into more JavaScript topics, check out the following articles:

Thank you for reading itsourcecoders 😊.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.
Caren Bautista

Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel  · View all posts by Caren Bautista →

Leave a Comment