[Label] [JavaScript] [The Defined Guide of JavaScript] How to declare a variable

Source: Internet
Author: User

Because I feel that my own JavaScript base is not solid, or can say there is no so-called foundation, so I have been watching the Defined guide of JavaScript recently.

While watching, I also made some reading notes, which are all from the book, I just extract some of the content, ready to use the content of each part of the notes to be published as a topic in the personal blog.

Declaration of a variable
In a JavaScript program, you must declare (declare) a variable before it is used.
(If you do not explicitly declare a variable, JavaScript implicitly declares it.) )
Variables are declared with the keyword Var, as follows:
var i;
var sum;
A var keyword declares multiple variables: var i, sum;

Variable declarations and variable initialization bindings together:
var message = ' Hello ';
var i = 0, j = 0, k = 0;

If you do not specify an initial value for a variable with the VAR statement, the variable is declared,
But before it is saved to a value, its initial value is undefined.

Note that the Var statement can also be part of the For loop and For/in Loop, which makes the loop variable's deity a part of the loop syntax itself.
For example:
for (var i = 0; i < i++) document.write (i, ' <br> ');
for (var i = 0, j = ten; I <; i++, j--) document.write (i*j, ' <br> ');
for (var i in O) document.write (i, ' <br> ');

Variables declared by Var are permanent, meaning that deleting these variables with the delete operator throws an error.

Duplicate declarations and omissions of statements

Declaring the same variable multiple times with the Var statement is not only legal, but it does not cause any errors.
If a duplicate declaration has an initial value, then it assumes the role of an assignment statement.

If you try to read the value of an undeclared variable, JavaScript generates an error.
If you attempt to assign a value to a variable that is not declared with Var, javascript implicitly declares the variable. But be aware that
Implicitly declared variables are always created as global variables, even if the variable is used only within a function body.
Local variables are used only in one function, to prevent the creation of global variables (or the use of existing global variables) when local variables are created,
You must use the var statement inside the function body. Whether it is a global variable or a local variable, it is best to create it using the VAR statement.


[Label] [JavaScript] [The Defined Guide of JavaScript] How to declare a variable

Related Article

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.