Skip to content

Instantly share code, notes, and snippets.

@StuffbyYuki
Last active April 27, 2020 00:40
Show Gist options
  • Save StuffbyYuki/12c0ffb340504c67f6730de398547957 to your computer and use it in GitHub Desktop.
Save StuffbyYuki/12c0ffb340504c67f6730de398547957 to your computer and use it in GitHub Desktop.
JS - Get max or min value in an array
//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