Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>(1) JavaScript does not have block statement scope; rather, it will be local to the code that the block resides within. </p> <p>(2) Javascript's declaration of variables in a function scope, meaning that <strong><em>variables declared in a function are available anywhere in that function, even before they are assigned a value</em></strong>.</p> <p>(3) Within the body of a function, a local variable takes precedence over a global variable with the same name. <strong>If you declare a local variable or function parameter with the same name as a global variable, you <em>effectively hide the global variable</em></strong>.</p> </blockquote> <p>you code is same as: (<em>read comment</em>)</p> <pre><code>&lt;script&gt; var a = 1; //global a = 1 function b() { a = 10; var a = 20; //local a = 20 } b(); alert(a); //global a = 1 &lt;/script&gt; </code></pre> <p>reference:<br> (1) <a href="http://docstore.mik.ua/orelly/webprog/jscript/ch04_03.htm" rel="nofollow"><em>JavaScript Variable Scope:</em></a><br> (2) <a href="http://thecomputersarewinning.com/post/a-dangerous-example-of-javascript-hoisting/" rel="nofollow"><em>A Dangerous Example of Javascript Hoisting</em></a><br> (3) <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Values,_variables,_and_literals#Variable_Scope" rel="nofollow"><em>Variable scope</em></a></p> <p>So in your code: </p> <pre><code>var a = 1; //global a = 1 function b() { a = 10; return; function a() {} //local } b(); alert(a); //global a = 1 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload