{"id":470,"date":"2022-08-30T15:01:53","date_gmt":"2022-08-30T15:01:53","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/javascript-unassigned-default-function-parameters-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:01:53","modified_gmt":"2022-08-30T15:01:53","slug":"javascript-unassigned-default-function-parameters-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/javascript-unassigned-default-function-parameters-collection-of-common-programming-errors\/","title":{"rendered":"javascript unassigned default function parameters-Collection of common programming errors"},"content":{"rendered":"<p>In the following function:<\/p>\n<pre><code>foo = function(a){\n    if (!a) a = \"Some value\";\n    \/\/ something done with a\n    return a;\n}\n<\/code><\/pre>\n<p>When &#8220;a&#8221; is not declared I want to assign a default value for use in the rest of the function, although &#8220;a&#8221; is a parameter name and not declared as &#8220;var a&#8221;, is it a private variable of this function? It does not seem to appear as a global var after execution of the function, is this a standard (i.e. consistent) possible use?<\/p>\n<ol>\n<li>\n<p>It&#8217;s a private variable within the function scope. it&#8217;s &#8216;invisible&#8217; in the global scope.<br \/>\nas for your code you better write like this<\/p>\n<pre><code>foo = function(a){\n    if (typeof(a)==\"undefined\") a = \"Some value\";\n    \/\/ something done with a\n    return a;\n}\n<\/code><\/pre>\n<p>because <code>!a<\/code> can be true for <code>0<\/code>, an <code>empty string ''<\/code> or just <code>null<\/code>.<\/p>\n<\/li>\n<li>\n<p>Yes, in this context <code>a<\/code> has a scope inside the function. You can even use it to override global variables for a local scope. So for example, you could do <code>function ($){....}(JQuery);<\/code> so you know that <code>$<\/code> will always be a variable for the JQuery framework.<\/p>\n<\/li>\n<li>\n<p>Parameters always have private function scope.<\/p>\n<pre><code>var a = 'Hi';\nfoo = function(a) {\n    if (!a) a = \"Some value\";\n        \/\/ something done with a\n        return a;\n    };\nconsole.log(a); \/\/ Logs 'Hi'\nconsole.log('Bye'); \/\/ Logs 'Bye'\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 19:44:09. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>In the following function: foo = function(a){ if (!a) a = &#8220;Some value&#8221;; \/\/ something done with a return a; } When &#8220;a&#8221; is not declared I want to assign a default value for use in the rest of the function, although &#8220;a&#8221; is a parameter name and not declared as &#8220;var a&#8221;, is it [&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-470","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/470","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=470"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/470\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}