Home > Web Front-end > JS Tutorial > How to declare variables in javascript

How to declare variables in javascript

青灯夜游
Release: 2021-06-18 16:44:29
Original
6776 people have browsed it

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;".

How to declare variables in javascript

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.

方式一:var 变量名;
方式二:var 变量名1,变量名2,…,变量名n;
方式三:var 变量名1 = 值1,变量名2 = 值2,…,变量名n = 值n;
Copy after login

1) Use var to declare one variable at a time or multiple variables at a time. Use commas to separate different variables. For example:

var name; //一次声明一个变量
var name,age,gender; //一次声明多个变量
Copy after login

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:

var name = "张三"; //声明的同时初始化变量
var name = "张三",age = 20,gender; //在一条声明中初始化部分变量
var name = "张三",age=20,gender = ’女’; //在一条声明中初始化全部变量
Copy after login

3) The specific data type of the variable is determined according to the data type of the assigned value, for example:

var message = "hello";//值为字符串类型,所以message变量的类型为字符串类型
var message = 123; //值为数字类型,所以message变量的类型为数字类型
Var message = true;//值为布尔类型,所以message变量的类型为布尔类型
Copy after login

4) In practical applications, the declaration of the loop variable is often directly as part of loop syntax. For example:

for(var i=0;i<10;i+=){…}。
Copy after login

[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:

方式一:let 变量名;
方式二:let 变量名1,变量名2,…,变量名n;
方式三:let 变量名1=值1,变量名2=值2,…,变量名n=值n;
Copy after login

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:

let age;
let age = 32,name = "Tom";
Copy after login

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:

const 变量名 = 值;
Copy after login

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:

const pi = 3.1415;
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template