Home  >  Article  >  Web Front-end  >  What are the ways to define variables in javascript

What are the ways to define variables in javascript

青灯夜游
青灯夜游Original
2021-04-09 16:12:268915browse

How to define variables in js: 1. Use var to define variables. The variables can be modified or not initialized. If not initialized, undefined will be output; 2. Use const to define variables. The variables cannot be modified and must be initialized; 3. Use let to define variables. The variables are used inside the function and have no effect outside the function.

What are the ways to define variables in javascript

The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.

The purpose of defining variables

Allocate a storage space in the memory to the variable to facilitate future data storage.

How to define variables?

Define variables (declare variables). Any variable must be defined before use. If multiple variables are defined, different storage spaces will be allocated for these variables.

You can use keywords in javascript: const, var, let to define variables.

1. Use var to define variables

Variables defined by var can be modified. If not initialized, undefined will be output and no error will be reported.

//未定义情况下
console.log(a); //打印undefined
//初始化
var a = "aaa";
console.log(a); //打印 aaa
//修改变量
var a = "bbb";
console.log(a); //打印bbb
//循环,检测块级作用域
for(i = 0; i < 5; i++) {
console.log(i); //依次打印0,1,2,3,4
setTimeout(function() {                
//定义函数内部变量        
console.log(i);               
var  d = "locald";            
}, 1000); //依次打印0,1,2,3,4
}          
//访问内部变量//设置定时器,因为要一秒后d才会被定义
setTimeout(function() {
console.log(d)
}, 1001); //报错,未被定义

[Recommended learning: javascript advanced tutorial]

2. Use const to define variables

Variables defined by const are not It can be modified and must be initialized. const defines a constant constant

//未定义情况下
console.log(a); //报错。没有定义,所以需要先定义
//初始化
const a = "aaa";
console.log(a); //打印 aaa
//修改变量
const a = "bbb";
console.log(a); //报错,a早已赋值,无法修改
//循环,检测块级作用域
for(i = 0; i < 5; i++) {
console.log(i); //依次打印0,1,2,3,4
setTimeout(function() {
 //定义函数内部变量
console.log(i);
const d = "locald";
}, 1000); //依次打印0,1,2,3,4
}
//访问内部变量//设置定时器,因为要一秒后d才会被定义
setTimeout(function() {
console.log(d)
}, 1001); //报错,未被定义

3. Use let to define variables

let is a block-level scope and is used internally in functions After let is defined, it has no impact on the outside of the function.

//未定义情况下
console.log(a); //报错,需要先定义
//初始化
let a = "aaa";
console.log(a); //打印aaa
//修改变量
let a = "bbb";
console.log(a); //打印a已经被声明了
//循环,检测块级作用域
for(i = 0; i < 5; i++) {
console.log(i); //依次打印0,1,2,3,4  
setTimeout(function() {
//定义函数内部变量
console.log(i);
let d = "locald";
}, 1000); //依次打印0,1,2,3,4
}
//访问内部变量//设置定时器,因为要一秒后d才会被定义
setTimeout(function() {
console.log(d)
}, 1001); //报错,未被定义

Attention, pay attention to the printout of the for loop, which is very different from var

The benefits of block-level scope are highlighted here

More For more programming related knowledge, please visit: programming video! !

The above is the detailed content of What are the ways to define variables in javascript. 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