{"id":13609,"date":"2022-03-25T13:41:52","date_gmt":"2022-03-25T13:41:52","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=13609"},"modified":"2024-02-16T10:44:28","modified_gmt":"2024-02-16T10:44:28","slug":"function-declaration-vs-function-expression-js","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=13609","title":{"rendered":"Function &#8211; declaration vs. expression (JS)"},"content":{"rendered":"<p><strong>Function declaration<\/strong><\/p>\n<pre class=\"brush: jscript; gutter: false; title: ; notranslate\" title=\"\">\r\nfunc1(); \/\/ works properly\r\n\/\/ func2(); \/\/ not defined\r\n\r\nfunction func1() {\r\n    console.log(&quot;func1&quot;);\r\n\r\n    func2(); \/\/ works properly\r\n    function func2() {\r\n        console.log(&quot;func2&quot;);\r\n    }\r\n    func2(); \/\/ works properly\r\n}\r\n\r\nfunc1(); \/\/ works properly\r\n\/\/ func2(); \/\/ not defined\r\n<\/pre>\n<p>They are hoisted, which means the declaration is &#8220;moved&#8221; to the top of the scope.<\/p>\n<hr>\n<p><strong>Function expression<\/strong><\/p>\n<pre class=\"brush: jscript; gutter: false; title: ; notranslate\" title=\"\">\r\n\/\/ func1(); \/\/ not defined\r\n\/\/ func2(); \/\/ not defined\r\n\r\nconst func1 = function() {\r\n    console.log(&quot;func1&quot;);\r\n\r\n    \/\/ func2(); \/\/ not defined\r\n    const func2 = function() {\r\n        console.log(&quot;func2&quot;);\r\n    }\r\n    func2(); \/\/ works properly\r\n}\r\n\r\nfunc1(); \/\/ works properly\r\n\/\/ func2(); \/\/ not defined\r\n<\/pre>\n<p>They aren&#8217;t hoisted, which means the declaration is NOT &#8220;moved&#8221; to the top of the scope.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Function declaration They are hoisted, which means the declaration is &#8220;moved&#8221; to the top of the scope. Function expression They<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false},"categories":[55],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/13609"}],"collection":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=13609"}],"version-history":[{"count":1,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/13609\/revisions"}],"predecessor-version":[{"id":16827,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/13609\/revisions\/16827"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13609"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}