- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
Advertisements