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 comments

2

u/[deleted] Apr 01 '18

[removed] — view removed comment

0

u/[deleted] Apr 01 '18

[deleted]

4

u/eattherichnow Apr 01 '18 edited Apr 01 '18

Look, const sounds new, and therefore hype, and a thing being hyped then it's wrong. Frankly, anything liked by more than two people is bad and wrong, and if one of them posted something about it to Reddit in 2009 then they're bad, wrong and can't shut up about it.

Edit: I forgot to mention that I am very smart.

1

u/tencircles Apr 01 '18

var is lexical scope, not global scope.

1

u/slikts Apr 02 '18

Not sure what OP meant, but for the sake of accuracy, var a = 1; outside of function scope will assign to window.a, i.e., the global object, so var is either function or globally scoped, while const or let are never global.

0

u/ghostfacedcoder Apr 01 '18

So a JS developer has two options right?

  1. Only use const for FOO_BAR = 5 type constants, gaining the "meta information" that said variables are "real constants" whenever you see const

  2. Use const by default for all variables that aren't re-assigned, and use the FOO_BAR variable naming convention for "real constants", giving you the exact same "meta information" about the exact same variables, but also protecting your other variables from accidental re-assignment.

Both Google and the significant majority number of respected JS thought leaders all say to go with #2, but as with anything in programming you're always welcome to ignore the experts and do things your way.

-1

u/[deleted] Apr 01 '18

[removed] — view removed comment

3

u/ghostfacedcoder Apr 01 '18 edited Apr 01 '18

You still can, you just have to change the const to let.

The idea is that you can accidentally assign to a variable that shouldn't be re-assigned, but you will never accidentally change a const to let, so by starting with const (except for variables you know you want to re-assign) you ensure everything is only re-assignable on purpose.

The only cost of doing things that way is that every once in awhile you have to change some const variables to let variables ... but that should be pretty rare, because you only have to when you started out thinking a variable wouldn't be re-assigned, but then you changed your mind later on. So while the benefit isn't that great, the cost is so low most people figure "why not get a little extra safety?"

0

u/slikts Apr 02 '18

It's safe to say that Google's or airbnb's style guides were made by language experts, so bringing up learning in this context is presumptuous.

A basic reason to avoid mixing let and var is consistency; the same code shouldn't use different kinds of variable declarations, unless there's a good reason for it, which there isn't for var; it's just not useful to treat function scoped variables differently. The differences also go beyond just block vs. function scoping; var declarations are also hoisted and they assign properties to the global object outside of functions.