How does the Math.max.apply()
work?.
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<script>
var list = ["12","23","100","34","56",
"9","233"];
console.log(Math.max.apply(Math,list));
</script>
</body>
</html>
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max
The above code finds the Max number in the List. Can anyone tell me how does the below code work?. It seems it works if i pass null or Math.
console.log(Math.max.apply(Math,list));
Does all the user-defined/Native functions
have call and apply method which we can use?.
apply
accepts an array and it applies the array as parameters to the actual function. So,can be understood as,
So,
apply
is a convenient way to pass an array of data as parameters to a function. Rememberwill not work, because
max
doesn't accept an array as input.There is another advantage, of using
apply
, you can choose your own context. The first parameter, you pass toapply
of any function, will be thethis
inside that function. But,max
doesn't depend on the current context. So, anything would work in-place ofMath
.Since
apply
is actually defined inFunction.prototype
, any valid JavaScript function object, will haveapply
function, by default.Math.max(val1, val2,...)
Math.max(1, 2, 3); // Math.max([value1[, value2[, ...]]])
value1, value2...
are parameters and must be Numbers MDN Math.maxYou can't pass
array
asMath.max
parameters. To pass an array you have to useapply
.Apply
The first parameter of apply is
this
, the second isarray
. Math methods are equivalent of static in other languages, which means it doesn't need an object instance unlike toarray.prototype.slice
so you don't need to passthis
in this case.Example
When you want to pass an array as parameter you have to use
apply
, otherwise usecall
.apply = array
call = comma separated
More about call & apply
Invokes a
Math.max
function withMath
object to be used as athis
reference in the function implementation (body) andlist
to be passed as an arguments.So this eventually equals to
Obviously
Math.max
implementation doesn't use instance variable - there is no reason to do so. The naive implementation would just iterate overarguments
and find the maximum one.Yes, every single function can be invoked using
call
orapply
References:
.apply()
documentation (credits to @RGraham)I will start by saying
Math.max(...numbers)
andFunction.prototype.apply()
should only be used for arrays with relatively few elements. (...) and apply will either fail or return the wrong result if the array is too largeMath.max.apply(null | undefined | Math, numbers)
is the same asMath.max(...numbers)
so I would recommendMath.max(...numbers)
for aesthetic reasons.If you need to find the maximum element in a numeric array that is very large: use the
Array.reduce()
method.Array.reduce()
can be used to find the maximum element in a numeric array, by comparing each value:Conclusion:
Numeric array that is relatively small: use
Math.max(...numbers)
Numeric array that is very large: use theArray.reduce()
methodOn JavaScript ES6 just use the Spread operator: