

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript - array undefined element count
We are required to write a JavaScript function that takes in an array of elements. The array of elements might contain some undefined values as well.
Our function should count the length of the array and the count should only contain the count of defined elements.
Example
The code for this will be −
const arr = [12, undefined, "blabla", ,true, 44]; const countDefined = (arr = []) => { let filtered; filtered = arr.filter(el => { return el !== undefined; }); const { length } = filtered; return length; }; console.log(countDefined(arr));
Output
And the output in the console will be −
4
- Related Questions & Answers
- JavaScript reduce sum array with undefined values
- JavaScript undefined Property
- Undefined in JavaScript
- Sorting an array that contains undefined in JavaScript?
- How to remove blank (undefined) elements from JavaScript array - JavaScript
- Undeclared vs Undefined? In JavaScript
- How to create undefined "holes" in an array in JavaScript?
- How to find the biggest number in an array around undefined elements? - JavaScript
- How to count JavaScript array objects?
- Searching an element in Javascript Array
- Swap kth element of array - JavaScript
- Group by element in array JavaScript
- JavaScript One fourth element in array
- First element and last element in a JavaScript array?
- Remove '0','undefined' and empty values from an array in JavaScript
Advertisements