Skip to content

All topics  /  Node.js

How to Add Multiple Elements to a List in Node JS?

Node.js ·

Hi Friends,

In this tutorial, you will learn how to add multiple elements to a list in node js. you can understand the concept of add multiple elements to a list in node.js. In this article, we will implement a node js to add multiple elements to a list. This post will give you a simple example of add multiple elements to a list in node js.

There are many ways to add multiple elements to a list in node js. i will give you three examples using push() , concat and add array to multiple elements add in node js.

let's see below a simple example with output:

Install Node JS


This step is not required; however, if you have not created the node js app, then you may go ahead and execute the below command:

mkdir my-app
cd my-app

 
npm init

Example: 1

index.js

// create array list
arr1 = [1, 2, 3];
// add array list
arr2 = ['hello',arr1, 'world'];
console.log(arr2);

Output:

[ 'hello', [ 1, 2, 3 ], 'world' ]

Example: 2

index.js

//create array list 1
myArray1 = [1, 2, 3, 4]
//create array list 2
myArray2 = [5, 6, 7]

 
//add new multiple elements to list
newArray = myArray1 +','+ myArray2

  
console.log(newArray)

Output:

1,2,3,4,5,6,7

Example: 3

index.js

//create array list
myArray = ['a'];
//add new multiple elements to list
myArray.push('b', 'c', 'd');
console.log(myArray);

Output:

[ 'a', 'b', 'c', 'd' ]