+ 6
is it a good practice to use only let and const in JavaScript without using var?
12 odpowiedzi
+ 8
No. Var is still valid and it is useful in specific cases. Best practice is to understand the differences between the different variable declarations and to know when to use the appropriate one. Let and const are block scoped. So a code like:
{
let data = 10;
}
console.log(data);
will throw an error.
Imagine this code is part of an object or function. A better option here would be to use var.
{
var data = 10;
}
console.log(data);
// 10
If you would like to learn more I recommend reading 'You Don't know JavaScript Yet' by Kyle Simpson 'Getting Started'. You can find a free online copy on Github. If you interested I will forward a link.
+ 4
let and const are supported by all modern browsers and are part of the ECMAScript 2015 (ES6) specification.
Basically if you don't need to support anything below IE11, let and const are safe to use nowadays.
Come on guys, it's 2020 now, why are we still afraid of using JavaScript 2015 syntaxes?
+ 4
Yes
+ 2
Calviղ not typically afraid to use it, but I needed to be sure of the pros and cons of using/not using it.
as for IE, that browser sucks, it makes you add more irrelevant lines to your code just to support it.
+ 2
GeraltdeRivia For react, we should use es6 codes, since Babel would handle the code transpile part.
+ 2
Why would you ride on a old bicycle when you have a new one?? let, const are the part of new modern JavaScript so avoid using var. But there is an issue with modern JavaScript, it is not supported by all the browsers specially Internet explorer (why does it even exist). IE doesn't support ES6 at all. But that's not a big issue because you could use babel to compile your ES6, ES7 code into es5.
+ 1
You do need a transpiler like babel if you want to support es5 browsers though
+ 1
In my short react course the teacher was against var. As it adds side-effects and let and const avoid those side effects.