Latest web development tutorials
×

JavaScript Tutorial

JavaScript Tutorial JavaScript Introduction JavaScript usage JavaScript Output JavaScript grammar JavaScript Statement JavaScript Notes JavaScript variable JavaScript type of data JavaScript Object JavaScript function JavaScript Scope JavaScript event JavaScript The string JavaScript Operator JavaScript Comparison JavaScript If...Else JavaScript switch JavaScript for JavaScript while JavaScript Break & Continue JavaScript typeof JavaScript Type conversion JavaScript Regular Expressions JavaScript error JavaScript debugging JavaScript Variable promotion JavaScript Strict mode JavaScript Use errors JavaScript Form validation JavaScript Keep the keyword JavaScript JSON JavaScript void JavaScript Code specification

JS function

JavaScript Function definition JavaScript Function parameter JavaScript Function call JavaScript Closure

JS HTML DOM

DOM Introduction DOM HTML DOM CSS DOM event DOM EventListener DOM element

JS Advanced Tutorial

JavaScript Object JavaScript Number JavaScript String JavaScript Date JavaScript Array JavaScript Boolean JavaScript Math JavaScript RegExp Object

JS Browser BOM

JavaScript Window JavaScript Window Screen JavaScript Window Location JavaScript Window History JavaScript Navigator JavaScript Pop-ups JavaScript Timing events JavaScript Cookies

JS Library

JavaScript Library JavaScript test jQuery JavaScript test Prototype

JS Examples

JavaScript Examples JavaScript Object instance JavaScript Browser object instance JavaScript HTML DOM Examples JavaScript to sum up

JS Reference Manual

JavaScript Object HTML DOM Object

JavaScript variable lift

JavaScript, the function and the variable declaration will be raised to the top of the function.

JavaScript, variables can be declared after use, that is, variables can be declared before the first use.

The following two examples will get the same result:

Example 1

x = 5; // variable x is set to 5

elem = document.getElementById ( "demo") ; // Find element
elem.innerHTML = x; // X is displayed in the element

var x; // declare x

try it"

Example 2

var x; // declare x
x = 5; // variable x is set to 5

elem = document.getElementById ( "demo") ; // Find element
elem.innerHTML = x; // X is displayed in the element

try it"

To understand the above examples you need to understand "hoisting (variable lift)."

Variable Lift: function declarations and variable declarations will always be the interpreter quietly been "promoted" to the top of the method body.


JavaScript initialization will not improve

JavaScript variable declarations will only increase, not initialized.

Results The following two examples are not the same:

Example 1

var x = 5; // initialize x
var y = 7; // initialize y

elem = document.getElementById ( "demo") ; // Find element
elem.innerHTML = x + "" + y ; // display the x and y

try it"

Example 2

var x = 5; // initialize x

elem = document.getElementById ( "demo") ; // Find element
elem.innerHTML = x + "" + y ; // display the x and y

var y = 7; // initialize y

try it"

Y Example 2 is outputundefined, this is because the variable declaration (var y) has improved, but the initialization (y = 7) does not increase, so the variable y is an undefined variable.

Example 2 is similar to the following code:

var x = 5; // 初始化 x
var y;     // 声明 y

elem = document.getElementById("demo"); // 查找元素
elem.innerHTML = x + " " + y;           // 显示 x 和 y

y = 7;    // 设置 y 为 7

Declare your variables in the head

For most programmers do not know JavaScript variable lift.

If the programmer is not well understood variables to enhance their written procedures prone to some problems.

To avoid these problems, we usually declare these variables before the beginning of each scope, this is normal JavaScript parsing step, easy to understand us.

Note JavaScript strict mode (strict mode) is not allowed to use an undeclared variable.
In the next chapter we will learn to "strict mode (strict mode)".