Table of Content
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)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
JavaScript switch statement checks many values without long chains. It appeared to replace stacked conditions that slow you down. Understand…
Math.asin() finds the arc sine of a number in JavaScript. It helps you work with angles from values. You get…
String Operators in JavaScript help you work with text. You can join, build, or change strings. Most of what you…
JavaScript Math.tan() finds the tangent of an angle in radians. You use it when you need to solve angle-based math.…
AJAX keeps your web app fast. You do not need to reload the page every time you send or get…
The toString function in JavaScript converts a value to a string form without change. It works on numbers, arrays, and…
JavaScript added Math.cbrt to solve cube roots. Before that, developers used custom code or Math.pow with 1/3. It works with…
JavaScript object methods are simple ways to handle data inside objects. An object can hold many values, and methods give…
You will face hoisting early when you write JavaScript. It affects how your code runs, and it may confuse you…
JavaScript syntax is what you will use when writing and reading javascript code, it is the foundation of using this…