+ 6

is it a good practice to use only let and const in JavaScript without using var?

2nd Jun 2020, 10:27 AM
Lucky victory
Lucky victory - avatar
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.
2nd Jun 2020, 11:29 AM
Ryan Els
Ryan Els - avatar
2nd Jun 2020, 11:39 AM
Ryan Els
Ryan Els - avatar
+ 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?
2nd Jun 2020, 1:37 PM
Calviղ
Calviղ - avatar
+ 4
Yes
3rd Jun 2020, 4:14 AM
Sonic
Sonic - avatar
+ 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.
2nd Jun 2020, 2:05 PM
Lucky victory
Lucky victory - avatar
+ 2
GeraltdeRivia For react, we should use es6 codes, since Babel would handle the code transpile part.
2nd Jun 2020, 4:18 PM
Calviղ
Calviղ - avatar
+ 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.
7th Jun 2020, 6:33 PM
Roni Dey
Roni Dey - avatar
+ 1
Ryan Els Gordon thanks for informations
2nd Jun 2020, 1:10 PM
Lucky victory
Lucky victory - avatar
+ 1
You do need a transpiler like babel if you want to support es5 browsers though
2nd Jun 2020, 2:23 PM
Ore
Ore - avatar
+ 1
In my short react course the teacher was against var. As it adds side-effects and let and const avoid those side effects.
2nd Jun 2020, 4:12 PM
GeraltdeRivia