{"id":756,"date":"2015-03-19T06:38:45","date_gmt":"2015-03-19T06:38:45","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=756"},"modified":"2015-03-19T06:38:45","modified_gmt":"2015-03-19T06:38:45","slug":"function-declaration-vs-named-function-expression-javascript","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/function-declaration-vs-named-function-expression-javascript\/","title":{"rendered":"Function declaration vs Named function expression Javascript"},"content":{"rendered":"<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\/\/Function declaration\r\nfunction foo() { return 5; }\r\n\r\n\/\/Anonymous function expression\r\nvar foo = function() { return 5; }\r\n\r\n\/\/Named function expression\r\nvar foo = function foo() { return 5; }\r\n<\/pre>\n<p>function declarations loads before any code is executed.While function expressions loads only<br \/>\nwhen the interpreter reaches that line of code.<\/p>\n<p>So if you try to call a function expression before it&#8217;s loaded, you&#8217;ll get an error<\/p>\n<p>But if you call a function declaration, it&#8217;ll always work. Because no code can be called until all declarations are loaded. <\/p>\n<p><strong>Function Expression<\/strong><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n  alert(foo()); \/\/ ERROR! foo wasn't loaded yet\r\n  var foo = function() { return 5; } \r\n<\/pre>\n<p><strong>Function Declaration<\/strong><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n  alert(foo()); \/\/ Alerts 5. Declarations are loaded before any code can run.\r\n  function foo() { return 5; } \r\n<\/pre>\n<p>The difference is in First case foo() is defined at run-time, whereas in second case foo() is defined at parse-time<br \/>\nfor a script block<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/\/Function declaration function foo() { return 5; } \/\/Anonymous function expression var foo = function() { return 5; } \/\/Named function expression var foo = function foo() { return 5; } function declarations loads before any code is executed.While function expressions loads only when the interpreter reaches that line of code. So if you try&hellip; <a href=\"https:\/\/codethataint.com\/blog\/function-declaration-vs-named-function-expression-javascript\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-756","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/756","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=756"}],"version-history":[{"count":1,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/756\/revisions"}],"predecessor-version":[{"id":757,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/756\/revisions\/757"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=756"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=756"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=756"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}