Js the front to get started --JavaScript declare variables

A piece of data can be stored by variables, the popular talk is that the data is stored in a variable.

As we all know, the data is stored in the computer's memory, how may be stored in a variable.

In fact, a variable is referenced memory address, and the name refers to a brief and understandable placeholder for ease of reference to modify the data.

This article will explain in detail how to declare a JavaScript variable points to its attention through code examples.

A variable declaration:

Before ES2015, only through var to declare variables, there is no other way.

With the development of version ES2015, and let the new const declare variables, constants can be considered a special variable.

Many tutorials statement that the use or non-use var var can declare a variable, do not use var declared global.

This article can responsibly say that this argument is wrong, before ES2015, only through var variable is declared.

Code examples are as follows:

var webName="兴趣部落";

webName and assign the string "ant tribe" by var to declare variables, the string in quotes.

Of course, other data types can be assigned, code examples are as follows:

var num=5;
var b=true;
var arr=[];

The code statement three variables are assigned numeric, boolean and an array of objects.

var webName="蚂蚁部落", num=5 , b=true;

You can declare multiple variables simultaneously and assignments, separated by commas between the variables.

Variables can be declared at the same time assignment, or you can declare an assignment after assignment or not.

Code examples are as follows:

var webName="兴趣部落";
var num,b;
num=5;
console.log(webName);
console.log(num);
console.log(b);

Code run effects shots are as follows:


If you do not assign values to variables, then the system will automatically assign a value to undefined.

Variable names are case sensitive, variable A and a are two different variables, code examples are as follows:

var a=5;
var A=6;
console.log(a);
console.log(A);

Effect theme code runs as follows:

After declaring a variable of the same name will overwrite the first variable declarations, and if a variable A is the same, the final print result is 6.

Second variable naming rules:

Named variables need to follow certain rules, otherwise it will error:

(1) The first character must be a letter, underscore (_) or dollar sign ($).

(2). The first character may be outside the underscore, or dollar sign any letter or number.

However, the variable name can not be reserved words or keywords that have a special meaning in JavaScript or future Habitat has a special meaning.

For example, the variable name can not be var or let, it is easy to understand.

Three pre-variable declaration:

Having the effect of the pre-declared variable var, that variable may be used first, in the statement.

To stress, and by variables or constants let const statement did not pre-effect.

The following code sample through a simple phenomenon at the front:

console.log(webName);
func();
var webName="兴趣部落";
function func(){console.log("函数")}

JavaScript code is executed from top to bottom order, call functions and can be printed and before the variable declaration.

It looks a bit weird, this is called pre-effect variables and function declarations.

Finally, I recommend a push in the front-end learning exchange group 685 910 553 Advanced ( front-end information-sharing ), no matter which direction you are on Earth,
whether you are welcome to work in a few years you settled in! (Within the group regularly offer free collection of some of the main group of free learning materials and books tidied face questions and answers document!)

If you have any objection to this article, then please write your comments in the article comments at.

If you find this article interesting, please share and forward, or you can look at, you acknowledge and encouragement of our articles.

Wish everyone in the programming this road, farther and farther.

Guess you like

Origin blog.csdn.net/tianduantoutiao/article/details/90813738