Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Post Timeline

Commonmark migration
Source Link

That's because your variable gets ["hoisted" up of its containing scope][1]"hoisted" up of its containing scope by the interpreter when you declare it. So your code ends up being interpreted like this:

function abcd()
{
 var a;
 alert(a); //alerts undefined
 a = 5;
}

To avoid this kind of confusion, you can follow some practices that will keep things in place, like declaring your local-scoped (that is, variables declared with the keyword var inside a function scope) variables right in the beginning of the function.

Note that, as you can read from the article, this happens with nested functions aswell. [1]: http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html

That's because your variable gets ["hoisted" up of its containing scope][1] by the interpreter when you declare it. So your code ends up being interpreted like this:

function abcd()
{
 var a;
 alert(a); //alerts undefined
 a = 5;
}

To avoid this kind of confusion, you can follow some practices that will keep things in place, like declaring your local-scoped (that is, variables declared with the keyword var inside a function scope) variables right in the beginning of the function.

Note that, as you can read from the article, this happens with nested functions aswell. [1]: http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html

That's because your variable gets "hoisted" up of its containing scope by the interpreter when you declare it. So your code ends up being interpreted like this:

function abcd()
{
 var a;
 alert(a); //alerts undefined
 a = 5;
}

To avoid this kind of confusion, you can follow some practices that will keep things in place, like declaring your local-scoped (that is, variables declared with the keyword var inside a function scope) variables right in the beginning of the function.

Note that, as you can read from the article, this happens with nested functions aswell.

added 319 characters in body
Source Link
Natan Streppel
  • 5.9k
  • 6
  • 39
  • 43

That's because theyour variable gets "hoisted" up of its containing scope ["hoisted" up of its containing scope][1] by the interpreter when you declare it. So your code ends up being interpreted like this:

function abcd()
{
 var a;
 alert(a); //alerts undefined
 a = 5;
}

To avoid this kind of confusion, you can follow some practices that will keep things in place, like declaring your local-scoped (that is, variables declared with the keyword var inside a function scope) variables right in the beginning of the function.

Note that, as you can read from the article, this happens with nested functions aswell. [1]: http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html

That's because the variable gets "hoisted" up of its containing scope by the interpreter when you declare it. So your code ends up being interpreted like this:

function abcd()
{
 var a;
 alert(a); //alerts undefined
 a = 5;
}

That's because your variable gets ["hoisted" up of its containing scope][1] by the interpreter when you declare it. So your code ends up being interpreted like this:

function abcd()
{
 var a;
 alert(a); //alerts undefined
 a = 5;
}

To avoid this kind of confusion, you can follow some practices that will keep things in place, like declaring your local-scoped (that is, variables declared with the keyword var inside a function scope) variables right in the beginning of the function.

Note that, as you can read from the article, this happens with nested functions aswell. [1]: http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html

Source Link
Natan Streppel
  • 5.9k
  • 6
  • 39
  • 43

That's because the variable gets "hoisted" up of its containing scope by the interpreter when you declare it. So your code ends up being interpreted like this:

function abcd()
{
 var a;
 alert(a); //alerts undefined
 a = 5;
}
lang-js

AltStyle によって変換されたページ (->オリジナル) /