{"id":9364,"date":"2013-01-30T19:29:14","date_gmt":"2013-01-30T13:59:14","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=9364"},"modified":"2016-08-30T11:13:41","modified_gmt":"2016-08-30T05:43:41","slug":"difference-between-call-and-apply-method-of-javascript","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/difference-between-call-and-apply-method-of-javascript\/","title":{"rendered":"Difference between call() and apply() method of JavaScript"},"content":{"rendered":"<p>Many people get confused with these two <a title=\"Javascript development services\" href=\"http:\/\/www.tothenew.com\/front-end-angularjs-development\">functions in JavaScript<\/a>, most of the time people think that we can pass an object in <strong>apply()<\/strong> and access it with <strong>this\u00a0<\/strong>which is not possible with the <strong>call()\u00a0<\/strong>method. But that is not the case, let&#8217;s see an example which will make it more clear.<\/p>\n<h3>Using call() method:<\/h3>\n<p>[js]<br \/>\n(function sum(a,b,c) {<br \/>\n var i, k=0;<br \/>\n var num = arguments.length;<br \/>\n for (i = 0; i < num; i++) {\n k+= arguments[i];\n }\n console.log(this.toString()); \/\/prints body of the function passed i.e., test()\n console.log(k); \/\/prints the sum in console\n this(); \/\/ this will call the test() function passed to sum\n return k; \/\/returns sum\n\n}).call(function test() {\n console.log(10); \/\/prints 10 in console\n },10,100);\n[\/js]\n\n\n\n<h3>Using apply() method:<\/h3>\n<p>[js] (function sum(a,b) {<br \/>\n    var i, k=0;<br \/>\n    var num = arguments.length;<br \/>\n    for (i = 0; i<num; i++) {\n        k+= arguments[i];\n    }\n    console.log(this.toString()); \/\/prints body of the function passed i.e., test()\n    console.log(k);               \/\/prints the sum in console\n    this();             \/\/ this will call the test() function passed to sum\n    return k;           \/\/returns sum\n\n}).apply(function test() {\n       console.log(10); \/\/prints 10 in console\n    },[10,100,200,200]);[\/js]\n\nTry it online at <a href=\"http:\/\/www.node-console.com\/script\/call-and-apply-functions-of-javascript\" target=\"_blank\">Node Console<\/a><\/p>\n<p>So the basic difference between both the methods is that in <strong>call()<\/strong> method we have to pass <strong>comma separated arguments<\/strong> and in <strong>apply()<\/strong> method we have to pass an array. If you have any queries then do let me know in comments section below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Many people get confused with these two functions in JavaScript, most of the time people think that we can pass an object in apply() and access it with this\u00a0which is not possible with the call()\u00a0method. But that is not the case, let&#8217;s see an example which will make it more clear. Using call() method: [js] [&hellip;]<\/p>\n","protected":false},"author":63,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0},"categories":[1],"tags":[1125,630,55,1124],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/9364"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=9364"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/9364\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=9364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=9364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=9364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}