JavaScript Array Count: How to Count Elements in Array

The count of an array can be found using the length property, also known as the JavaScript Array Length. The length property can do multiple jobs, including returning or setting the number of elements.

JavaScript Array Count

To count elements of an array in JavaScript, use the length property. The length property sets or returns the number of items in an array. The value of a length property is the integer with a positive sign and a value less than 2 to the 32nd power.

Syntax

array.length

To set the length of the array, use the array.length =number syntax.

Return Value

The length property returns a Number, representing the number of elements in the array object.

Example

const netflix = ["Stranger Things", "Money Heist", "Loki", "WandaVision"]

let length = netflix.length

console.log(length)

Output

4

It returns 4, which means the array contains four elements. The length property of an object which is an instance of type Array sets or returns the number of elements in that array.

RangeError in JavaScript

A RangeError Object is thrown when you try to pass a value as an argument to a function that does not allow a range that includes the value.

let listC = []
listC.length = Math.pow(2, 32) - 1
console.log(listC.length)

let listA = new Array(4294967296)
let listB = new Array(-100)

console.log(listA.length)
console.log(listB.length)

Output

4294967295

let listA = new Array(4294967296)
 ^
RangeError: Invalid array length
 at Object.<anonymous> (/Users/krunal/Desktop/code/node-examples/es/app.js:9:13)

You can see that we got a RangeError when the array length is invalid.

You can set the length property to truncate an array at any time. When you extend an array by changing its length property, the number of actual elements increases.

const arr = [11, 21]
console.log(arr)

arr.length = 5
console.log(arr)

Output

[ 11, 21 ]

[ 11, 21, <3 empty items> ]

How to count certain elements in an array

To count certain elements in the array in JavaScript, use the for loop and array.length property.

const arr = [11, 21, 19, 18, 46]
let count = 0

for (let i = 0; i < arr.length; ++i) {
 if (arr[i] == 21)
 count++;
}

console.log(count)

Output

1

In this example, we are counting 21 element. It appeared 1 time in the array that is why it returns 1.

Using filter() and a length property

If you don’t want to use the for loop to count the specific elements in the array, you can use the combination of the filter() method and a length property.

const arr = [11, 21, 19, 21, 46]

let count = arr.filter(x => x == 21).length

console.log(count)

Output

2

The “21” element is appeared 2 times in the array, so it returns 2 as an output.

Creating a count() prototype

You can create a prototype function of count() that will take the specific number we want to count and returns the count.

Object.defineProperties(Array.prototype, {
 count: {
   value: function(value) {
   return this.filter(x => x==value).length;
   }
  }
});

const arr = [11, 21, 19, 21, 46]
let count = arr.count(21)
console.log(count)

Output

2

That’s it for this tutorial.

Leave a Comment