var is not a new feature of es6; you can use the var keyword to declare a variable. The declared variable is both a global variable and a top-level variable. It can be initialized or not assigned a value. The initialized assignment can be of any type. , if no value is assigned, it is the default value.
The operating environment of this tutorial: Windows 10 system, ECMAScript version 6.0, Dell G3 computer.
Use the var keyword to declare a variable, which can be initialized or not assigned. The initialized assignment can be of any type. If not assigned, it is The default value is undefined. The var declaration is generally in the global scope and is added as a property of the window (except those declared in the function, which are function scope).
var
Variables declared with var are both global variables and top-level variables (in the browser environment, the top-level object refers to the window object, in node refers to the global object)
Variables that use var life may be subject to variable promotion
Using var can perform multiple operations on a variable declaration, the variable declared later will overwrite the previous variable declaration
When var is used to declare a variable in a function, the variable is local; if var is not used in the function, the variable is changed It is global
Compared with let, let is a new feature of es6
let
is a new command in es6, used to declare variables.
is similar to var in usage, but the declared variable is only valid within the code block where the let command is located, not There is variable promotion; as long as there is a let command in the block-level scope, this area will no longer be affected by the outside.
Before using let to declare a variable, the variable is not available, which is what everyone often says Temporary dead zone (let does not allow repeated declarations in the same scope, so we cannot re-declare parameters inside the function)
var declares variables
<script> /* *1.使用var 声明变量,可以保存任意数据类型的值, * Undefined、Null、Number、String、Symbol、Boolean、Object 7种类型 *2.在同一代码块位置: * var userName; userName="marshal" * var userName="marshal" 是完全等价有效的 * var 可以以逗号分隔,定义多个变量,分号结束 * var userName,userAge,userAddress; */ var userName="marshal";//var userName;userName="marshal"; console.log(userName); </script>
Use developer tools to view the following picture:
var scope description
Scope: at runtime , the visibility of access to variables, objects, functions and other resources in a code area. JavaScript is divided into two types, global and local. It is at the same level as the Script tag and can be understood as global. In the browser, these are called windows objects, so global variables and functions defined using var are all properties and methods of the window object. Local is defined within {} curly braces, such as inside a function, and defined in the local scope. Generally speaking, it cannot be accessed outside the function, but it can be solved through closure (more on this later).
<script> /* * 1.定义函数localVariable * 2.在函数内部使用var 声明变量userName,并设置值为marshal * */ function localVariable(){ var userName="marshal"; console.log("函数内部访问"+userName);//输出函数内部访问marshal } localVariable(); console.log("函数外部访问"+userName); //报错:UncaughtReferenceError: userName is not defined </script>
After the function localVariable() is called, the variables and objects declared using var inside the inner function are destroyed, so an error is reported: UncaughtReferenceError: userName is not defined
If var is omitted inside the function , it is a global variable. The sample code is as follows:
<script> /* * 1.定义函数localVariable * 2.在函数内部省去var关键字 * 3.注不建议省去var 关键字,局部的全局变量很难维护,容易埋坑 */ function localVariable(){ userName="marshal"; console.log("函数内部访问"+userName); } localVariable();//函数调用后,userName变为全局变量,可使用window对象访问。 console.log("函数外部访问"+window.userName); </script>
[Related recommendations: javascript video tutorial, web front-end]
The above is the detailed content of Is var a new feature of es6?. For more information, please follow other related articles on the PHP Chinese website!