How to Get Length of Array in Node JS?

Aug 04, 2022

.

Admin

How to Get Length of Array in Node JS?

Hello Friends,

This tutorial will provide an example of how to count a number of elements in a list in node js. We will look at an example of node.js count the number of items in the list. you will learn how to count the number of elements in a list in node js. I want to share the node js count of all elements in a list.

This example is how to count the number of elements in a list in node js. There are many ways to find the length of the list in node js. i will give you two examples using length() and filter() count all elements of list 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

//An Array is defined with 5 instances

myArray = [10,20,30,40,50];

//Now arr.length returns 5

countArray = myArray.length;

console.log(countArray);

Output:

5

Example: 2

index.js

myArray = [1, 2, 3, 5, 2, 8, 9, 2];

// Count how many 2 there are in arr

countArray = myArray.filter(x => x === 2).length;

console.log(countArray);

Output:

3

#Node JS