JavaScript Variables: var and let and const

栏目: IT技术 · 发布时间: 4年前

内容简介:There are three ways to create variables in a JavaScript application: usingWhen you declare a variable withWhen you declare a variable with

There are three ways to create variables in a JavaScript application: using var , using let , or using const . This will not be a post trying to convince you which one you should use, or arguing about what is best. It’s just good to know about the differences and what it means when you use the different options. But hopefully by the end of all this you’ll be comfortable with the three options and can make a decision for your team that will suit your needs. To get the most out of this post, it is best if you understand variable scope, which we covered inthis post previously.

When you declare a variable with var , the variable will be function scoped. If you try and use the variable before it’s declared in that function, it will have an undefined value due to hoisting. These may be desired effects for your app, but my recommendation is to not use var for variable declarations.

When you declare a variable with let , the variable will be block scoped. If you try to use the variable before it’s declared in that block, or outside that block, a ReferenceError will be thrown. This is nice because the occasions should be rare that you use a variable before declaring it. These variables are also re-assignable. Let’s look at an example:

function playGame() {
	let gameName = 'Super Mario Brothers';

	gameName = 'Mario Kart 8';

	console.log(gameName); // Mario Kart 8
}

playGame();

In the above example, we declare the gameName variable and initialize its value to Super Mario Brothers . But right below it, we reassign the value to a different string, Mario Kart 8 . This is completely valid for variables declared with let . My recommendation is to use let any time you need to reassign the value of a variable. Examples of when you might need to do this would be when in a for loop, for example.

Variables using const

When you declare a variable with const , the variable will be block scoped. If you try to use the variable before it’s declared in that block, or outside that block, a ReferenceError will be thrown. This is nice because the occasions should be rare that you use a variable before declaring it. The difference between const and let , though, is that variables declared with const can not have their values reassigned. So our above example would produce a TypeError for trying to assign a value to a constant variable. Let’s look at an example of ways that you can change parts of a const variable:

const game = {
	title: 'Zelda: Breath of the Wild',
};

game.title = `Zelda: Link's Awakening`;

console.log(game.title); // Zelda: Link's Awakening

Even though game was declared using const , we can still change the value of an attribute on the object. The same is true with arrays. You can push onto an array declared with const . What you can’t do is the following:

const game = {
	title: 'Zelda: Breath of the Wild',
};

game = {
	title: `Zelda: Link's Awakening`,
}; // TypeError: Assignment to constant variable

In the second example, the error occurs because we are changing the value of the game variable itself, not just one of its attributes’ values.

So which of the three options should you use? Well, I’ll let you and your team decide. I personally like to use const all the time, unless in the case where I know I’ll need to reassign the value of a variable. In those cases I use let . I think it helps your future self and other developers know that you didn’t intend for that value to change, and the application will produce an error if you do try to change it. But if you would rather use let , then go ahead and do what works best for you and your team.


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

科技想要什么

科技想要什么

[美] 凯文·凯利 / 熊祥 / 中信出版社 / 2011-11 / 58.00元

在《科技想要什么》一书中,凯文•凯利向我们介绍了一种全新的科技观。他认为,作为整体,科技不是由线路和金属构成的一团乱麻,而是有生命力的自然形成的系统,它的起源完全可以回溯到生命的初始时期。正如生物进化呈现出无意识的趋势,科技也是如此。通过追踪这些长期趋势,我们可以对“科技想要什么”有所理解。 凯文•凯利预测了未来数十年科技的12种趋势,包括创造大脑这一得寸进尺之举。不过,为了让人类创造的世界......一起来看看 《科技想要什么》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具