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.
Table of Content
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)arraymeans the target array.element1, element2, ..., elementNare 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) // 4This 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) // 2This 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
Logical operators in JavaScript let you check conditions and combine multiple checks in your code. In this guide, you will…
JavaScript switch statement checks many values without long chains. It appeared to replace stacked conditions that slow you down. Understand…
JavaScript gives you the Math.round() function to deal with decimal numbers. You use it when you want to round a…
JavaScript arithmetic operators let you add, subtract, multiply, or divide numbers. You can also use them to find remainders or…
If you are a coder, one of your primary requirements is to have a trustworthy code- editor. A Code editor…
The Math.acos function finds the angle in radians from a cosine value in JavaScript. It gives a value between 0…
JavaScript runs code in different ways, but the for loop stays one of the most common tools. You use it…
The toSpliced function creates a new array without changing the original array in JavaScript. It returns a copy with new…
Unary operators in JavaScript work with only one value. They can change, test, or change the type of that value,…
JavaScript moves fast, and new features come. Some browsers do not support these features and cause issues for users. Developers…