Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Node.js unshift() function

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

unshift() is an array function from Node.js that is used to insert elements to the front of an array. 

Syntax:

array_name.unshift(element)

Parameter: This function takes a parameter that has to be added to the array. It can take multiple elements also as parameters.

Return type: The function returns the array after adding the element. The program below demonstrates the working of the function: 

Example 1: 

javascript




function unshiftDemo() {
    const element = arr.unshift(12);
    console.log(arr);
}
const arr = [1, 5, 7, 9, 78];
unshiftDemo();


Output:

[12,1,5,7,9,78]

Example 2: 

javascript




function addLang() {
    const element = arr.unshift("Node.js", "php");
    console.log(arr);
}
const arr = ["C", "Java", "JavaScript", "Python"];
addLang();


Output:

[ 'Node.js', 'php', 'C', 'Java', 'JavaScript', 'Python' ]
My Personal Notes arrow_drop_up
Last Updated : 30 Mar, 2023
Like Article
Save Article
Similar Reads
Related Tutorials