Home  >  Article  >  Web Front-end  >  What are variables in javascript

What are variables in javascript

青灯夜游
青灯夜游Original
2021-09-01 17:04:303466browse

In JavaScript, a variable is a container that temporarily stores values. It can store numbers, text, or some complex data, etc.; and the variable name is the label attached to the container. The variable can be found through the label, so that Read and write the value it stores.

What are variables in javascript

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

What are variables?

Literally speaking, a variable is a variable quantity; from a programming perspective, a variable is a container that temporarily stores values. It can store numbers, text, or some complex data, etc. The variable name is the label attached to the container. The variable can be found through the label so that the value it stores can be read and written.

Take two boxes as an example. To distinguish them, one is represented by box1 and the other is represented by box2. Of course, you can also use any name to distinguish them. This box1 is the name of the box, which is also the name of the variable.

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: const, var, let to define variables in javascript, syntax:

关键字 变量名称;

Example:

var name;
var name, age, sex;

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); //报错,未被定义

2. Use const to define variables

Variables defined by const cannot 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 Defining variables

let is a block-level scope. After being defined using let inside the function, 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); //报错,未被定义

Pay 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

[ Recommended learning: javascript advanced tutorial

The above is the detailed content of What are 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