Search code examples
javascriptcall

Difference between fn() and fn.call() in JavaScript


var tempFn = function(someText){
console.log(someText);
}

tempFn('siva');
// where I simply call the function with text 'siva'

vs.

tempFn.call(this,'siva');
// where I call the function using call method

What is the difference between these approaches?


Solution

  • When you use the call form you are being explicit about the context that the function will be invoked with.

    The context will determine what the value of this is when your function executes.

    In your case, you are passing in this which would be the default anyway, so it's a no-op. Also, your tempFn function doesn't invoke the this keyword so it wouldn't matter anyway if you passed in a different scope.