Last active
April 27, 2020 00:40
-
-
Save StuffbyYuki/12c0ffb340504c67f6730de398547957 to your computer and use it in GitHub Desktop.
JS - Get max or min value in an array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//The follwoing will work | |
var nums = [1, 2, 3] | |
Math.min.apply(Math, nums) // 1 | |
Math.max.apply(Math, nums) // 3 | |
Math.min.apply(null, nums) // 1 | |
Math.max.apply(null, nums) // 3 | |
//This doesn't work | |
var nums = [1, 2, 3] | |
Math.min(nums) // NaN | |
Math.max(nums) // Nan | |
//That is because Math.min or Math.max functions expect distinct variables and not an array. | |
//So in order to do that before ES6/ES2015 apply method was used |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment