{"id":670,"date":"2022-08-30T15:05:13","date_gmt":"2022-08-30T15:05:13","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/difference-between-assigning-function-to-variable-or-not-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:05:13","modified_gmt":"2022-08-30T15:05:13","slug":"difference-between-assigning-function-to-variable-or-not-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/difference-between-assigning-function-to-variable-or-not-collection-of-common-programming-errors\/","title":{"rendered":"Difference between assigning function to variable or not-Collection of common programming errors"},"content":{"rendered":"<p>The main difference is the first one (a function declaration) is <em>hoisted<\/em> to the top of the scope in which it is declared, whereas the second one (a function expression) is not.<\/p>\n<p>This is the reason you are able to call a function that has been declared after you call it:<\/p>\n<pre><code>testFunction();\nfunction testFunction() {}\n<\/code><\/pre>\n<p>You can&#8217;t do that with a function expression, since the assignment happens in-place:<\/p>\n<pre><code>testFunction();\nvar testFunction = function() {}; \/\/TypeError\n<\/code><\/pre>\n<p>There is also a third form (a <em>named<\/em> function expression):<\/p>\n<pre><code>var testFunction = function myFunc() {};\n<\/code><\/pre>\n<p>In this case, the identifier <code>myFunc<\/code> is only in scope inside the function, whereas <code>testFunction<\/code> is available in whatever scope it is declared. <em>BUT<\/em> (and there&#8217;s always a but when it comes to Internet Explorer) in IE below version 9 the <code>myFunc<\/code> identifier wrongly leaks out to the containing scope. Named function expressions are useful when you need to refer to the calling function (since <code>arguments.callee<\/code> is deprecated).<\/p>\n<p>Also note that the same is true for variable declarations:<\/p>\n<pre><code>console.log(x); \/\/undefined (not TypeError)\nvar x = 10;\n<\/code><\/pre>\n<p>You can imagine that the JavaScript engine interprets the code like this:<\/p>\n<pre><code>var x; \/\/Declaration is hoisted to top of scope, value is `undefined`\nconsole.log(x);\nx = 10; \/\/Assignment happens where you expect it to\n<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2013-11-09 21:21:11. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>The main difference is the first one (a function declaration) is hoisted to the top of the scope in which it is declared, whereas the second one (a function expression) is not. This is the reason you are able to call a function that has been declared after you call it: testFunction(); function testFunction() {} [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-670","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/670","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=670"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/670\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=670"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=670"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=670"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}