The example in this article describes the method of javascript variable declaration. Share it with everyone for your reference. The specific analysis is as follows:
A variable should be declared before using it in js. Variables are declared using the keyword var.
If an initial value is not specified for a variable in the var declaration statement, the variable value is undefined.
There is no need to specify the variable type when declaring the variable. JS variables can be of any data type.
It is legal and harmless to declare variables repeatedly using the var statement. If the repeated declaration has an initializer, it is no different from a simple assignment statement.
If you try to read an undeclared variable, js will report an error. In ECMAScript5 strict mode, assigning a value to an undeclared variable will also report an error; however, historically, in non-strict mode, if you assign a value to an undeclared variable, js will actually create a property with the same name for the global object, and It seems like it works like a properly declared global variable. This means you can get away with not declaring global variables, but this is a bad habit that can cause a lot of bugs, and it's better to always use var to declare variables.
In the function body, local variables with the same name will overwrite global variables.
Although you can write code in the global scope without writing the var statement, you must use the var statement when declaring local variables. Please refer to the following code:
scope = "global"; function foo(){ scope="local" //fk!我们刚刚修改了全局变量!!! }
In programming languages like C, each piece of code within curly braces has its own scope, and variables are not visible outside the code section where they are declared. We call this block scope. ); There is no block-level scope in js, but function scope is used instead: variables are defined in the function body in which they are declared and any function body in which this function body is nested (whether Inner nesting or outer nesting? )
The function scope of js means that all variables declared within the function are always visible within the function body, which means that the variables can even be used before they are declared. This feature of js is informally called declaration hoisting, that is, all variables declared in a js function (but not assigned) are "advanced" to the top of the function body.
var scope = "global"; function f(){ console.log(scope); //输出"undefined"而不是"global" var scope = "local"; //变量在这里赋初始值,但变量在函数体内任何地方均是有定义的 console.log(scope); //输出"local"
The above code is equivalent to:
function f(){ var scope; console.log(scope); scope = "local"; console.log(scope); }
When declaring a js global variable, it actually defines a property of the global object.
When you declare a variable with var, the attribute created is not configurable, that is, it cannot be deleted with the delete operator; but if you do not use strict mode and assign a value to an undeclared variable, js will automatically create a global variable. Variables created in this way are normal configurable properties of the global object and can be deleted:
var x = 1; y = 2; this.z = 3; //同上 delete x; //返回false,无法删除变量 delete y; //返回true,变量被删除 delete this.z //同上
I hope this article will be helpful to everyone’s JavaScript programming design.