{"id":576,"date":"2022-08-30T15:03:39","date_gmt":"2022-08-30T15:03:39","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/declaring-a-javascript-function-that-is-inside-a-variable-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:03:39","modified_gmt":"2022-08-30T15:03:39","slug":"declaring-a-javascript-function-that-is-inside-a-variable-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/declaring-a-javascript-function-that-is-inside-a-variable-collection-of-common-programming-errors\/","title":{"rendered":"Declaring a JavaScript function that is inside a variable?-Collection of common programming errors"},"content":{"rendered":"<p>They do almost the same thing, but there are differences and reasons for having both. The first a function expression because it is assigned to a variable. In addition, unrelated to being a function expression, it is an anonymous function because it does not have a function name. The second one is a function declaration because it is not part of another expression, and is &#8220;a source element (non-nested statement in the script or a body function) of a function of the script.&#8221; (I don&#8217;t quite know how to explain a source element so I copied that from Mozilla docs). It also has a function name, myFunction. In addition, the function can also be used before the function declaration, unlike functions defined by a function expression.<\/p>\n<p>The following example using a function expression will not work:<\/p>\n<pre><code>plusOne(1);\nvar plusOne = function (x) {\n    return x + 1;\n};\n<\/code><\/pre>\n<p>The following using a function declaration instead, will work:<\/p>\n<pre><code>plusOne(1);\nfunction plusOne(x) {\n    return x + 1;\n};\n<\/code><\/pre>\n<p>In addition, seeing as the topic of the link where you saw this function declaration is closures, I&#8217;ll continue on to explain why you may use a function expression and an anonymous function. Sometimes, a function simply doesn&#8217;t need a name and can just be anonymous because no one will need to call it by its name. A function expression can be used for something you might have learned about yet, called closures. A closure is when a function encloses variables for use within it at a later time, preventing any outer scope from reaching it. They are a bit difficult to explain, and can also be tough to understand without going through various examples a few times and trying it yourself.<\/p>\n<p>Assuming you have enough basic javascript knowledge (for loops, creating and appending dom elements, and onclick), take for example the following code, which is similar to a problem I encountered before I discovered closures myself. Read through it without running the code first, and try to figure out what will happen when you click each button\/div.<\/p>\n<pre><code>function addButtons() {\n    for (var i=0; i<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2013-11-09 21:03:36. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>They do almost the same thing, but there are differences and reasons for having both. The first a function expression because it is assigned to a variable. In addition, unrelated to being a function expression, it is an anonymous function because it does not have a function name. The second one is a function declaration [&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-576","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/576","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=576"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/576\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}