Create a simple array using node.js


Overview

Array can be created in node.js in any of the ways given below:

Code


													
//Name of the file : create-array.js
// an array of names
var names = ["rj", "ricky", "alex"];
console.log(names);
//calculating the length of array
var len = names.length;
console.log(len);	
//Another way to create array 
var arr = new Array(3); 	// declare an array "arr" of size 3
arr = [1,5,7];  		// initialize elements of array

//OR
// declare and initialize in a single statement
var arr1 = new Array(2,5,7); 
console.log("arr : " + arr);
console.log("arr 1 : " + arr1);
													
												

Run

  • Now run the snippet using the following command :
    													
    >node create-array.js
    [ 'rj', 'ricky', 'alex' ]
    3
    arr : 1,5,7
    arr 1 : 2,5,7