最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

In Javascript, can I use a variable before it is declared? - Stack Overflow

programmeradmin16浏览0评论

I have been wondering for a while if I can use a variable in JS before it is defined, such as the following:

var country = "USA";
switch (country) {
    case "USA":
        country = i;
    case "blach":
        //not finished yet
}
/*
  put a whole
  bunch more code here
*/
var i = 10;

Is this valid? Is it allowed? And if so, what is the technical term for it?

I have been wondering for a while if I can use a variable in JS before it is defined, such as the following:

var country = "USA";
switch (country) {
    case "USA":
        country = i;
    case "blach":
        //not finished yet
}
/*
  put a whole
  bunch more code here
*/
var i = 10;

Is this valid? Is it allowed? And if so, what is the technical term for it?

Share Improve this question edited Mar 20, 2014 at 10:32 Mark Walters 12.4k6 gold badges35 silver badges48 bronze badges asked Nov 26, 2013 at 15:12 tjonstjons 4,7695 gold badges29 silver badges36 bronze badges 1
  • Sorry, but I felt that this question does not "show enough research effort". Variable scope is fundamental programming knowledge. Simply running the code in question should have proved that is was allowed. The question is probably "what is the technical term for using variables before they are declared". Feel free to edit the question and the downvote might go away. I will point out that even this JavaScript question has no mention of hoisting. – andyb Commented Nov 26, 2013 at 15:36
Add a comment  | 

5 Answers 5

Reset to default 38

This is a technique used by the JavaScript engine called hoisting. The parser will read through the entire function before running it, and any variable declarations (i.e. using the var keyword) will be executed as if they were at the top of the containing scope. So your code behaves like:

var country;
var i;

country = "USA";
switch (country) {
case "USA":
    country = i;
case "blach":
    //not finished yet
}

i = 10;

So, i is declared throughout the entire scope, but its value is undefined until the i = 10 statement runs.

In ECMAScript terms, when a function is invoked, the function's new lexical scope builds its VariableEnvironment before any of the function's code runs. In ECMAScript 10.5, step 8:

8. For each VariableDeclaration... d in code, in source text order do

a. Let dn be the Identifier in d.

...

i. Call env’s CreateMutableBinding concrete method passing dn and configurableBindings as the arguments.

ii. Call env’s SetMutableBinding concrete method passing dn, undefined, and strict as the arguments.

This is quite a mouthful, but basically it says:

Before you run a function, look through the function's source code for declarations like var [identifierName].

For each declaration you find, create a new variable in the function's scope with the name [identifierName] used in the declaration and then set its value to undefined

It's called variable hoisting, and it's a good concept to understand as it can occasionally create bugs that are hard to track down.

For example:

var stuff = 'stuff';
function() {
 console.log(stuff); //outputs 'undefined'
 var stuff = 'other stuff';
 console.log(stuff); //outputs 'other stuff'
}

The first console.log outputs undefined because the var stuff in the function was hoisted to the top of the function.

//theoretical compiled output
var stuff = 'stuff';
function() {
 var stuff; //has not been defined
 console.log(stuff);
 stuff = 'other stuff'; //defined here
 console.log(stuff);
}

Without knowledge of variable hoisting, this result may be confusing.

For this reason, if you look at professionally developed JavaScript code, typically you'll see all variables in a function are declared at the top.

Yes. In JavaScript, variables are hoisted

A variable statement declares variables that are created as defined in 10.5. Variables are initialised to undefined when created. A variable with an Initialiser is assigned the value of its AssignmentExpression when the VariableStatement is executed, not when the variable is created.ES5 §12.2
Where 10.5 step 8 is the part of interest

The guys answers are correct. for your example however it is worth noting that country is undefined. as aspillers has mentioned your code behaves as below

var country;
var i;

country = "USA";

switch (country) {
case "USA":
    country = i;
case "blach":
    //not finished yet
}

i = 10;
alert(country) //undefined;

but when your case statement ran for "USA", i was undefined so this was assigned to country. try it here in this fiddle.

I guess that you just need to be aware that although the variable declarations are hoisted, the value assignments aren't.

Yes, you can use a variable before declaring it. It's called hoisting in JavaScript. One thing to keep in mind though is that only a variable's declaration is hoisted, not its initialization. It means that you'll not able to use the value of the variable which you'll assign later in the code. For example,

console.log(myVar); //undefined
myVar = 10;
var myVar;

So you'll not get the error (myVar is not defined) even when you're using myVar before declaring it. But the console logged value of myVar will be undefined. At the starting of code execution, variable was declared, but its assignment was not. So the JS engine would see the above code block as

var myVar;
console.log(myVar); //undefined
myVar = 10;
var myVar;

It will put the declaration for all the variables used in the file at the top of the code.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论