Why should I add the var keyword when declaring a variable in JavaScript? javascriptvar

Source: Internet
Author: User

Why should I add the var keyword when declaring a variable in JavaScript? javascriptvar

In JavaScript, var is used to declare a variable, but this syntax is not strictly required. If you modify it many times, you can directly use a variable instead of using var to declare it.

Copy codeThe Code is as follows:
Var x = "XX ";
Y = "xxx ";

And so on. This has a problem. For example, in a certain line of code, I want to use a declared variable x. As a result, the variable is written as y due to incorrect typing or spelling, the result is equivalent to "implicit" declaring a variable y. In actual programming, this error is sometimes hard to be found.
When you perform this "implicit" declaration in the current context, the JavaScript engine first checks whether the variable has been declared in the current context. If not, search in the context of the upper level. If it is not found, the variable will be declared in the window!
For example:

The Code is as follows:

window. y = "hello"; function func(){ y = "OH, NO!!!"; } func(); alert(window.y); //#=> display "OH, NO!!!" 

When any layer in the context contains variables defined by "implicit", the variable at this layer will be modified without generating a new variable on the window. (This bug is also annoying, especially the encapsulated complicated code)
For example:

The Code is as follows:

var x = "window.x"; function a() { var x = "a's x"; var b = function() { var c = function() { //no var! x = "c's x:"; }; alert("before c run,the b.x:" + x); c(); alert("after c run, the b.x:" + x); }; alert("a.x is:" + x); b(); alert("after b function runed, the a.x is:" + x); }; alert("before a run, window.x:" + x); a(); alert("after a run, window.x:" + x); 

There are the following layers: window, func a, func B, and func are always hierarchical nesting. Window-> a-> B-> c
In Windows and a, both the defined variable x and B do not define the variable. In c, the 'implicit 'declares an x, and the x finally modifies the value of the variable.
Remember, to declare a variable in JavaScript, you must add var first.


Can the var keyword be omitted when JavaScript declares variables or objects?

Comparison between using var Declaration for js declaration variables and without var Declaration

When using Javascript to declare variables, although the var keyword is used to declare the variables and the keyword is not used to declare the variables, it is not a problem to run in many cases, but the two methods are different. Code that can run normally does not mean it is suitable code.

In JS, variable declarations are divided into explicit and implicit declarations.
Var I = 100 // display declarative I = 100 // implicit declarative
In a function, variables explicitly declared using the var keyword are used as local variables without the var keyword. global variables are declared using direct value assignment.

When we access an unspecified variable, JS reports an error. When we assign a value to a variable without declaration, JS will not report an error. On the contrary, it will think that we need to implicitly declare a global variable, which is 1.1 important.
Thx


What is the difference between adding var in javascript and without declarative variables?

You can declare a global variable without declaring a variable in the var function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.