JavaScript unshift Function: How it Works with Examples

javascript unshift function

The JavaScript unshift function adds one or more elements at the start of an array. It shifts existing items to higher indexes and changes the array length. You need this function when you want new data at the front of a list.

Syntax of the unshift Function in JavaScript

The unshift function is part of the Array object.

Here is the syntax:

array.unshift(element1, element2, ..., elementN)
  • array means the target array.
  • element1, element2, ..., elementN are new elements you want to add.

The function returns the new length of the array after insertion.

Here is a quick example:

let items = [2, 3, 4]
let newLength = items.unshift(1)
console.log(items)      // [1, 2, 3, 4]
console.log(newLength)  // 4

This array had three numbers, and the unshift function added 1 at the start. It returned 4 because the new array length became four.

Add Multiple Elements with the unshift Function in JavaScript

You can insert many values in one call. Each value appears in the array in the same order you pass.

let fruits = ["mango", "banana"]
fruits.unshift("apple", "orange")
console.log(fruits) // ["apple", "orange", "mango", "banana"]

The array first had two fruits. The unshift function added apple then orange. Both values appeared at the start in the same order.

The unshift function does not return the new array. It returns only the new length after insertion.

let colors = ["blue", "green"]
let length = colors.unshift("red")
console.log(length)  // 3
console.log(colors)  // ["red", "blue", "green"]

The return value is the new length 3. The array itself changed because red was added at index 0.

The Difference Between unshift and push

The unshift function adds values at the start, while the push function adds values at the end.

let numbers = [2, 3]
numbers.unshift(1)
numbers.push(4)
console.log(numbers) // [1, 2, 3, 4]

This function places 1 at the start and the push function puts 4 at the end.

The unshift function can be slower than push. The function must move every existing element to a higher index. So large arrays with many elements can take more time. For small arrays, the difference is not visible.

You can use unshift when:

  • You need new items at the front of a list.
  • The order of data must keep new items first.
  • You want to handle small or medium arrays.

Do not use it when you must handle very large arrays because of the performance cost.

Examples

Add One Element:

let nums = [2, 3, 4]
nums.unshift(1)
console.log(nums) // [1, 2, 3, 4]

This adds 1 at the start of the array and shifts old elements forward.

Add Many Elements:

let fruits = ["mango"]
fruits.unshift("apple", "orange")
console.log(fruits) 

This adds apple and orange at the front of the array. Here is the output:

 ["apple", "orange", "mango"]

Get New Length:

let colors = ["blue"]
let length = colors.unshift("red")
console.log(length) // 2

This example shows 2 in the output because it returns the length after it adds red at index zero.

Wrapping Up

You understood how the unshift function works in JavaScript. Here is a quick recap:

  • The JavaScript unshift function adds elements at the start of an array.
  • It changes the array and returns the new length.
  • You can add one or many values.
  • It works best for small arrays when you need fresh data first.

FAQs

What does the JavaScript unshift function do?

  • The unshift() function adds elements at the start of an array.
  • It shifts old elements forward and changes array length.

let arr = [2, 3]
arr.unshift(1)
console.log(arr) // [1, 2, 3]

What is the difference between unshift and push?

  • unshift() adds items at the start of an array.
  • push() adds items at the end of an array.

let nums = [2, 3]
nums.unshift(1) // [1, 2, 3]
nums.push(4)    // [1, 2, 3, 4]

What does unshift return in JavaScript?

  • unshift() returns the new length of the array.
  • It does not return the new array itself.

let colors = ["blue"]
let length = colors.unshift("red")
console.log(length)  // 2
console.log(colors)  // ["red", "blue"]

Similar Reads

JavaScript Logical Operators with Examples

Logical operators in JavaScript let you check conditions and combine multiple checks in your code. In this guide, you will…

JavaScript switch Statement: Syntax and Examples

JavaScript switch statement checks many values without long chains. It appeared to replace stacked conditions that slow you down. Understand…

JavaScript Math round(): How It Works With Examples

JavaScript gives you the Math.round() function to deal with decimal numbers. You use it when you want to round a…

Understanding JavaScript Arithmetic Operators

JavaScript arithmetic operators let you add, subtract, multiply, or divide numbers. You can also use them to find remainders or…

Top 5 Code Editors: The Best IDE for JavaScript

If you are a coder, one of your primary requirements is to have a trustworthy code- editor. A Code editor…

JavaScript Math acos Function

The Math.acos function finds the angle in radians from a cosine value in JavaScript. It gives a value between 0…

JavaScript for Loop: How to Iterate Arrays by Examples

JavaScript runs code in different ways, but the for loop stays one of the most common tools. You use it…

JavaScript toSpliced Function Explained with Examples

The toSpliced function creates a new array without changing the original array in JavaScript. It returns a copy with new…

JavaScript Unary Operators: How they Work with Examples

Unary operators in JavaScript work with only one value. They can change, test, or change the type of that value,…

JavaScript Polyfilling & Transpiling Guide for Beginners

JavaScript moves fast, and new features come. Some browsers do not support these features and cause issues for users. Developers…

Previous Article

PHP array_find_key: How it Works with Examples

Next Article

How to Open Link in New Tab with HTML Target

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.