JavaScript变数

One of the most fundamental topics in any language is variables. Variables are the most primitive containers that hold our data. They allow us to store a value like a name, number, etc that we can use anytime later in the program. There are three ways to declare variables in JavaScript: using the let keyword, var keyword, and const keyword.

在任何语言中,最基本的主题之一就是变量变量是保存我们数据的最原始的容器。 它们允许我们存储一个值,例如名称,数字等,我们以后可以在程序中随时使用。 在JavaScript中可以通过三种方法声明变量 :使用let关键字var关键字const关键字

Open the chrome dev console to try out the examples by right-clicking on the browserselecting inspectselecting console or simply press f12.

右键单击浏览器选择检查选择控制台,打开chrome dev控制台以尝试示例,或者直接按f12键

Here you can define variables in JavaScript using the following syntax:

在这里,您可以使用以下语法在JavaScript中定义变量

    let variable_name = value 

Variables in JavaScrpt | 1

If we want to change the value assigned to a variable, we avoid the use of let keyword again. We simply use the assignment operator (=) and the variable takes the newly assigned value.

如果要更改分配给变量的值,则避免再次使用let关键字 。 我们只需使用赋值运算符(=) ,变量就会获得新分配的值。

Syntax:

句法:

    variable_name = new_value; 

We use the const keyword to declare constant. A constant is quite literally a value that is fixed or wouldn't change during the program. If we declare a variable as constant, we cannot and should not reassign a value to it.

我们使用const关键字声明常量。 常量从字面上看是固定的值,在程序执行过程中不会更改。 如果将变量声明为常量,则不能也不应为其重新分配值。

Variables in JavaScrpt | 2

Here we get an error because assigning a new value to a const is not allowed. Both the above keywords were added by ES16 update to the language and are fairly new. The old way of declaring variables is using the var keyword.

在这里我们得到一个错误,因为不允许将新值分配给const 。 上述两个关键字都是通过ES16更新添加到该语言的,并且是相当新的。 声明变量的旧方法是使用var关键字

Variables in JavaScrpt | 3

The major difference between let and var are their scopes. Variable declared with let has a block scope whereas var has function scope.

let和var之间的主要区别是它们的范围。 用let声明的变量具有块范围,而var具有函数范围。

Variables in JavaScrpt | 4

Redeclaring let variables gives an error because it is hoisted in a temporal dead zone unlike var is hoisted normally.

重新声明let变量会产生错误,因为将其悬挂在时间上的死区中,而不会像正常悬挂var那样悬挂。

Writing async functions using let is quite different. Look at the following example,

使用let编写异步函数非常不同。 看下面的例子,

for(var i=0; i<3; i++ ){
	setTimeout(function(){
		console.log(i);
	},1000)
}

for(var i=0; i<3; i++){
   setTimeout(function(){
       console.log(i);
   },1000)
}
//Output is 3

for(let i=0; i<3; i++){
   setTimeout(function(){
       console.log(i);
   },1000)
}
//Output is 0,1,2

Due to hoisting in the temporal dead zone, we get all the values of let barring the last value on the console. Whereas in the case of using var, we only get it's final value after the setTimeout() function executes (i.e. after 1 sec).

由于悬挂在暂时的死区中,因此我们获得了let的所有值,该值禁止控制台上的最后一个值。 而在使用var的情况下,我们只能在setTimeout()函数执行后(即1秒钟后)获得它的最终值。

翻译自: https://www.includehelp.com/code-snippets/javascript-variables.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值