JavaScript is a weakly typed language and can be used directly without declaring variables. This is simple but not easy to find errors in variable names, so it is not recommended. It is common practice to declare JavaScript variables before using them. Currently, there are three JavaScript variable declarations: var, let, and const keyword declarations.

ECMAScript6 uses var to declare variables, which has been used in ECMAScript6. In this way, variables can cause problems in some cases, so ECMAScript6 uses let and const to declare variables.

JavaScript takes the form of weak data types, so JavaScript variables are free variables. It can accept any type of data during the operation of the program, no matter which way to declare, there is no need to specify the data type in the declaration, which is very different from strongly typed Java and other languages that need to specify the data type of variables.

Var, let, and const can all declare variables, but there are many differences between them.

Use var to declare variables

Var can be used to declare global or function-scoped variables in the following ways.

Method 1: var variable name;

Method 2: var Variable name 1, variable name 2...... , variable name n;

Var Variable name 1 = value 1...... , variable name n = value n,

**1) ** Var can be used to declare one variable at a time, or to declare multiple variables at a time, separated by commas. Such as:

01: var name; 02: var name,age,gender; // Declare multiple variables at onceCopy the code

** The value of a variable is undefined by default. You can also initialize variables at the same time you declare them. Such as:

01: var name = "zhang3"; 02: var name = "zhang", age = 20, gender; // Initialize some variables in a declaration 03: var name = "zhang", age = 20, gender = 'female '; // Initialize all variables in a single declarationCopy the code

** the data type of the variable is determined by the data type of the assigned value, for example:

01: var message = "hello"; // The value is a string, so the message variable is of type string. 02: var message = 123; // The value is numeric, so the message variable is of numeric type 03: var message = true; // The value is of Boolean type, so the message variable is of Boolean type.Copy the code

**4) ** In practice, the life of a loop variable is often directly referred to as part of the loop syntax. Such as:

01: for(var I = 0; i < 10 ; I +=) {...... }Copy the code

Use let to declare variables

Let can be used to declare block-level scoped variables in the same format as var variables. There are three ways to declare variables, as follows:

Method 1: let variable name;

Let variable name 1, variable name 2...... , variable name n;

Let variable name 1 = value 1, variable name 1 = value 1...... , variable name n = value n,

The syntax for declaring variables using let is exactly the same as for var. An example of declaring a variable using let is as follows:

01: let the age; 02: let age = 32, name = "Tom ";Copy the code

Declare a variable using const

Variables declared using var and let can change their values during the running of script code. If you want the value of a variable to remain constant throughout the entire run of your script, you need to declare it as const.

Const variable name = value;Copy the code

It is important to note that when you declare a variable using const, you must assign an initial value to the variable and that value cannot be changed throughout the course of the code. In addition, variables cannot be declared more than once. Failure to meet any of these requirements will result in an error.

An example of a const declaring a variable is as follows:

01: const pi = 3.1415;
Copy the code

Four, the difference between three variable declaration methods

**1) ** Variable initialization requirements are different: var and let can declare variables without initialization, the value of the uninitialized variable is “undefined”, the value of the variable can be changed during the operation of the code. Const variables must be initialized when they are declared, and cannot be modified during the entire run of the code, otherwise an error will be reported at run time.

2) Variable promotion is supported differently: var declarations support variable promotion, while const and let declarations do not. Variable promotion comes later.

3) Different support for block-level scope: var variables do not support block-level scope, let and const variables do support block-level scope. Any code enclosed by a pair of curly braces {} is called a code block. The so-called block-level scope refers to the effective scope of a code block, outside the code block, the variable will be invalid.

4) Duplicate declaration: Var can duplicate the same variable in the same scope. Let and const cannot duplicate the same variable.

Example code is as follows:

var gv1 = "JavaScript"; let gv2 = "JS"; const number = "10000"; var gv1 = "VBcript"; //① let gv2 = "JScript"; // const number = "20000"; / / (3) alert (gv1); alert(gv2); alert(number);Copy the code