It just depends on the structure of your arguments.
call is useful for manually calling a function where you have explicitly defined arguments.
call
const a = 'bar' const b = 'baz' foo.call({}, a, b)
But sometimes you have an array of arguments and you want to programmatically call a function with them, that's where apply is useful.
apply
const args = ['bar', 'baz'] foo.apply({}, args)
That said, with the advent of the spread/rest operator (...), apply is redundant.
...
const args = ['bar', 'baz'] // notice this is using call, not apply! foo.call({}, ...args)
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
It just depends on the structure of your arguments.
call
is useful for manually calling a function where you have explicitly defined arguments.But sometimes you have an array of arguments and you want to programmatically call a function with them, that's where
apply
is useful.That said, with the advent of the spread/rest operator (
...
),apply
is redundant.