Question about apply()

// my question
I wonder about declaring the variable in apply () function in the code below, why use “null” and what is its role?

// my code

var arr = [6, 89, 3, 45];
var maximus = Math.max.apply(null, arr); // returns 89

the first parameter is what this references to. Since your example doesn’t need to use this, the value null is given’

1 Like

Note that using ‘apply’ is an obsolete mechanism for this use case. These days, just use the spread operator to pass a variable number of args from an array:

Math.max(...arr)

1 Like