JS method of declaring variables: 1. Use the var keyword to declare, the syntax "var variable name;" or "var variable name = value"; 2. Use the let keyword to declare, the syntax "let variable name;" " or "let variable name = value"; 3. Use the const keyword to declare, the syntax is "const variable name = value;".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
JavaScript is a weakly typed language and can be used directly without declaring variables. Although this is simple, it is not easy to find errors in variable names, so it is not recommended. Common practice is to declare JavaScript variables before using them. Currently, there are three ways to declare variables in JavaScript, namely using the var, let and const keywords.
Among them, using var to declare variables has been the method used before the ECMAScript6 version. Since variables declared in this way can cause some problems in some cases, the use of let and const has been added in the ECMAScript6 version. There are two ways 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 running of the program. No matter which method is used to declare it, there is no need to specify the data type when declaring. This is very different from the variable declaration in strongly typed languages such as Java, which requires specifying the data type of the variable. Big difference.
Although var, let and const can all declare variables, there are many differences between them. These declaration methods will be introduced one by one below.
1. Use var to declare variables
Use var to declare variables in the global or function-level scope. There are several ways to declare the syntax.
1 2 3 |
|
1) Use var to declare one variable at a time or multiple variables at a time. Use commas to separate different variables. For example:
1 2 |
|
2) When declaring a variable, you do not need to initialize it (i.e. assign an initial value), in which case its value defaults to undefined; you can also initialize the variable while declaring it. For example:
1 2 3 |
|
3) The specific data type of the variable is determined according to the data type of the assigned value, for example:
1 2 3 |
|
4) In practical applications, the declaration of the loop variable is often directly as part of loop syntax. For example:
1 |
|
[Related recommendations: javascript learning tutorial】
2. Use let to declare variables
Using let, you can declare variables in block-level scope. The format of declaration is the same as the format of var declaration. There are three ways, as shown below:
1 2 3 |
|
The syntax description of using let to declare variables and var declaration The variables are exactly the same and will not be described again here. An example of using let to declare variables is as follows:
1 2 |
|
3. Use const to declare variables
The values of variables declared using var and let can be changed during the running of the script code. Change. If you want the value of a variable to remain unchanged during the entire running process of the script code, you need to use const to declare it. The declaration format is as follows:
1 |
|
Special attention should be paid to: when using const to declare a variable, you must assign it to the variable. The initial value, and this value cannot be modified during the execution of the entire code. In addition, variables cannot be declared multiple times. If any of these requirements are not met, an error will be reported.
Examples of using const to declare variables are as follows:
1 |
|
For more programming-related knowledge, please visit: Programming Video! !
The above is the detailed content of How to declare variables in javascript. For more information, please follow other related articles on the PHP Chinese website!