Home  >  Article  >  Web Front-end  >  What can I use to declare variables in javascript without using var?

What can I use to declare variables in javascript without using var?

青灯夜游
青灯夜游Original
2021-10-14 14:47:522116browse

Javascript declares variables without using var. You can use the let and const keywords. The two ways of declaring variables, let and const, are newly added in the ECMAScript6 version. Use let to declare variables in block-level scope. Variables declared with const are similar to let variables, but cannot be reassigned.

What can I use to declare variables in javascript without using var?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

There are three ways to declare variables in JavaScript, namely using the var, let and const keyword declarations. JavaScript does not use var to declare variables. You can also use the let and const keywords.

Among them, let and const are the two new ways to declare variables in the ECMAScript6 version.

1. Use var to declare variables

Use var to declare variables in 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;

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; //一次声明多个变量

2) When declaring a variable, you do not need to initialize it (that is, 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 = ’女’; //在一条声明中初始化全部变量

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变量的类型为布尔类型

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+=){…}。

2. Use let to declare variables

Use let to declare variables in block level scope. The format of declaration is the same as that of var declaration3. A way, as shown below:

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

The syntax description of using let to declare variables is exactly the same as that of using var to declare variables, so I won’t go into details here. An example of using let to declare variables is as follows:

let age;
let age = 32,name = "Tom";

3. Use const to declare variables

Variables declared using var and let are used during the running of the script code. , the value can be changed. 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 变量名 = 值;

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;

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of What can I use to declare variables in javascript without using var?. For more information, please follow other related articles on the PHP Chinese website!

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