Create An Array in Node.JS

Create An Array in Node.JS
Techiio-author
Written by Shuvhojit DebDecember 29, 2021
4 min read
NodeJs
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Shuvhojit Deb

Full Stack Developer

In this blog, we'll learn how to create an array in Node.Js.

How to Create Array in Node.Js

To create arrays, you can either use traditional notation or array literal syntax :

var arr1 = new Array();
var arr2 = [];

As with objects, the literal syntax version is preferred.

We can test if an object is an array using the Array.isArray function:

var arr2 = [];
Array.isArray(arr2);
Array.isArray({});

We can create arrays quite easily in JavaScript using [].

Arrays have many useful functions.

var myData = []; 
// w  ww. j a va  2  s.  c  o  m
myData.push(1); // add at the end 
console.log(myData); // prints [1] 
myData.unshift(2); // add to the top 
console.log(myData); // prints [2,1] 
// Arrays are zero index based: 
console.log(myData[0]); // prints 2 

The code above generates the following result.

[1]

[2, 1]

2

Example

The array type in the JavaScript length property returns the element count.

var arr2 = [];
arr2.length;
console.log(arr2);
var arr3 = [ 'cat', 'rat', 'bat' ];
console.log(arr3.length);

The code above generates the following result.

[]

3

By default, arrays in JavaScript are numerically indexed:

var arr3 = [ 'cat', 'rat', 'bat' ];
for (var i = 0; i < arr3.length; i++) {
    console.log(arr3[i]);
}

The code above generates the following result.

cat

rat

bat

To add an item to the end of an array, you can do one of two things:

var arr3 = [ 'cat', 'rat', 'bat' ];
arr3.push("mat");
console.log(arr3);
arr3[arr3.length] = "fat";
console.log(arr3);

The code above generates the following result.

['cat', 'rat', 'bat', 'mat']

['cat', 'rat', 'bat', 'mat', 'fat']

We can specify the index of the element where you want to insert a new element.

If this element is past the last element, the elements in between are created and initialized with the value undefined :

var arr3 = [ 'cat', 'rat', 'bat' ];
arr3[20] = "splat";
console.log(arr3);

The code above generates the following result.

['cat', 'rat', 'bat', , , , , , , , , , , 'splat']

Note

To delete an item from an array, use the splice function, which takes an index and the number of items to delete.

What it returns is an array with the extracted items, and the original array is modified such that they no longer exist there:

var arr3 = [ 'cat', 'rat', 'bat', 'cat', 'rat', 'bat' ];
arr3.splice(2, 2);
console.log(arr3);
console.log(arr3.length);

The code above generates the following result.

['cat', 'rat', 'rat', 'bat']

4

NodeJS
Array
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Shuvhojit Deb
Full Stack Developer
Techiio-followerTechiio-follower
117 Blog Posts
0 Discussion Threads
Trending Technologies
15
Software40
DevOps46
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance