Use length property to get Array count in JavaScript. A length property also used to iterate through by looking at the length property and Create an empty array of fixed lengths, etc.
Example count Array in JavaScript
HTML example code:
<!DOCTYPE HTML>
<html>
<body>
<script>
var arr = [1, 2, 3, 5, 2, 8, 9, 2];
var count = arr.length;
console.log(count);
</script>
</body>
</html>
Output:
Q: How to count certain elements in Array?
Answer: Let’s find how many 2s are in the Array:-
An array:
[1, 2, 3, 5, 2, 8, 9, 2]
It’s very simple with a for loop, match given item with all elements of Array and count it:
Output: 3
Do comment if you have any doubts and suggestions on this JS Array topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version