- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Articles
- JavaScript reduce sum array with undefined values
- Sorting an array that contains undefined in JavaScript?
- How to remove blank (undefined) elements from JavaScript array - JavaScript
- Undefined in JavaScript
- JavaScript undefined Property
- Undeclared vs Undefined? In JavaScript
- How to find the biggest number in an array around undefined elements? - JavaScript
- What is Undefined X1 in JavaScript
- How to count JavaScript array objects?
- Remove '0','undefined' and empty values from an array in JavaScript
- What is "undefined x 1" in JavaScript?
- Compute the sum of elements of an array which can be null or undefined JavaScript
- First element and last element in a JavaScript array?
- Searching an element in Javascript Array
- Swap kth element of array - JavaScript

Advertisements