
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to find the biggest number in an array around undefined elements? - JavaScript
We are required to write a JavaScript function that takes in an array that contains some numbers, some strings and some falsy values.
Our function should return the biggest Number from the array.
For example −
If the input array is the following with some undefined values −
const arr = [23, 'hello', undefined, null, 21, 65, NaN, 1, undefined, 'hii'];
Then the output should be 65
Example
Following is the code −
const arr = [23, 'hello', undefined, null, 21, 65, NaN, 1, undefined, 'hii']; const pickBiggest = arr => { let max = -Infinity; for(let i = 0; i < arr.length; i++){ if(!+arr[i]){ continue; }; max = Math.max(max, +arr[i]); }; return max; }; console.log(pickBiggest(arr));
Output
This will produce the following output on console −
65
- Related Articles
- How to remove blank (undefined) elements from JavaScript array - JavaScript
- Find unique and biggest string values from an array in JavaScript
- How to remove certain number elements from an array in JavaScript
- Sorting an array that contains undefined in JavaScript?
- Compute the sum of elements of an array which can be null or undefined JavaScript
- How to find the total number of elements in an array in TypeScript?
- How to count the number of elements in an array below/above a given number (JavaScript)
- JavaScript - array undefined element count
- How to duplicate elements of an array in the same array with JavaScript?
- Unique number of occurrences of elements in an array in JavaScript
- How to remove duplicate elements from an array in JavaScript?
- How to add new array elements at the beginning of an array in JavaScript?
- Finding two closest elements to a specific number in an array using JavaScript
- JavaScript - How to pick random elements from an array?
- How to find the largest number contained in a JavaScript array?

Advertisements