0

Ok, so this is confusing me. If I'm not wrong, the only difference between the two is that using apply the second argument can be an array. So, when I do this:

function calcMax(arr) {
    return arr
}

console.log(calcMax.apply(null, [1,2,3]))

OUPTUT = 1

but when I do this:

function calcMax(arr) {
    return arr
}

console.log(calcMax.call(null, [1,2,3]))

I get the desired output: [1,2,3]

why?

A R
  • 76
  • 1
  • 7
  • 1
    calcMax.apply is similar to `calcMax(1, 2, 3)`, calcMax.call is similar to `calcMax([1, 2, 3])` – Nick Parsons Mar 05 '20 at 12:24
  • 1
    Because that's exact;y how they work - `apply` will supply the second parameter as if it's the `arguments` supplied, while `call` will take each parameter from the 2nd onwards and apply *those* as the `arguments` – VLAZ Mar 05 '20 at 12:25
  • 1
    With Javascript any extra parameters are just ignored. So calcMax(1,2,3) is the same as calcMax(1), and calcMax.call treats the array as one parameter so it returns the array. – QuentinUK Mar 05 '20 at 12:28
  • "_second argument can be an array_" its not just any array, its single array of arguments, so you are basically passing 3 arguments to `calcMax` but accessing only first one, thus you get only 1 as result. you will get the desired result if you do `calcMax.apply(null, [[1,2,3]])` – palaѕн Mar 05 '20 at 12:31

0 Answers0