Open In App
Related Articles

Node.js push() function

Improve Article
Improve
Save Article
Save
Like Article
Like

push() is an array function from Node.js that is used to add elements to the end of an array. 

Syntax:

array_name.push(element)

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

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

Program 1: 

javascript




function PUSH() {
    arr.push(2);
    console.log(arr);
}
const arr = [12, 3, 4, 6, 7, 11];
PUSH();


Output:

[ 12,  3, 4, 6, 7, 11, 2]

Program 2: 

javascript




function addLang() {
    arr.push('DS', 'Algo', 'JavaScript');
    console.log(arr);
}
const arr = ['NodeJs'];
addLang();


Output:

[ 'NodeJs', 'DS', 'Algo', 'JavaScript' ]

Program 3: 

javascript




const Lang = ['java', 'c', 'python'];
 
console.log(Lang);
// expected output: Array [ 'java', 'c', 'python' ]
 
Lang.push('node');
 
console.log(Lang);
// expected output: Array [ 'java', 'c', 'python', 'node' ]


Output:

[ 'java', 'c', 'python' ]
[ 'java', 'c', 'python', 'node' ]

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 30 Mar, 2023
Like Article
Save Article
Previous
Next
Complete Tutorials