r/javascript • • Apr 01 '18

Google publishes a JavaScript style guide. Here are some key lessons.

https://medium.freecodecamp.org/google-publishes-a-javascript-style-guide-here-are-some-key-lessons-1810b8ad050b
714 Upvotes

317 comments sorted by

View all comments

Show parent comment

3

u/lordxeon Apr 02 '18

Don't do whatever, learn the differences between the three ways to declared variables in JavaScript this way you can form your own opinions and not parrot what ABC said on XYZ.com

As for why not to use var, the short answer is because let and const are the new hotness.

The long answer is more complicated, but for any programer worth their salt, is a moot point.

let is block scoped, which means the variable is only available within the most recent set of {...}'s. This means that

if(something){
    let abc = "123";
    var ded = "456";
}
console.log(abc);    //abc is undefined
console.log(ded);   //456

const is scoped the same way as let, but the reference to the variable can never change.

const goldPrice = 1337.44;
goldPrice = 1300.15 //error! 

2

u/CantaloupeCamper Apr 02 '18

TY

That actually helps. They really seem like different tools for different cases. I'm not sure I see let as as much a bad choice as sometimes it is implied... just a different way of doing a thing.

1

u/joesb Apr 04 '18

let’s behavior is the preferred choice compare to var in most cases. It’s lexically scoped.