- 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 reduce sum array with undefined values
We have an array of numbers that contains some undefined and null values as well. We are required to make a function, say quickSum that takes in the array and returns its quick sum, ignoring the undefined and null values.
The full code for doing so will be −
Example
const arr = [23,566,null,90,-32,undefined,32,-69,88,null]; const quickSum = (arr) => { const sum = arr.reduce((acc, val) => { return acc + (val || 0); }, 0); return sum; }; console.log(quickSum(arr));
Output
The output in the console will be −
698
- Related Articles
- Sorting Array with JavaScript reduce function - JavaScript
- Sum from array values with similar key in JavaScript
- JavaScript - array undefined element count
- Retaining array elements greater than cumulative sum using reduce() in JavaScript
- Reduce an array to the sum of every nth element - JavaScript
- Reduce array in JavaScript
- Merge two objects in JavaScript ignoring undefined values
- Remove '0','undefined' and empty values from an array in JavaScript
- Compute the sum of elements of an array which can be null or undefined JavaScript
- Finding the product of array elements with reduce() in JavaScript
- Sum all duplicate values in array in JavaScript
- Finding the sum of unique array values - JavaScript
- Filter unique array values and sum in JavaScript
- Sorting an array that contains undefined in JavaScript?
- How to remove blank (undefined) elements from JavaScript array - JavaScript

Advertisements