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

-14

u/lordxeon Apr 01 '18

Use const by default

I will get downvoted for this, but (like everything) JavaScript developers take a good idea, and destroy it. Function level variables should not be a const. Const variables should be those that never change during the run-time of the application.

15

u/ghostfacedcoder Apr 01 '18

Why not? I can accidentally assign to a variable inside a function can't I? Const vars prevent that from happening, and cost you nothing (except having to type one extra character vs. let).

-2

u/lordxeon Apr 01 '18

Function level variables

I don't think variables declared inside a function should be declared with const. Any global, or global equivalent are fine being const if necessary.

And, they do cost you something because unless you're running the ES6 code by default, it's being converted back into vars by babel anyway.

2

u/ghostfacedcoder Apr 01 '18

Maybe now, but that's going the way of the dodo/IE box model. Chrome already supports const and let, and I'm pretty sure the other major browsers do too, so it's only a matter of time until all the older versions fall out of use and const/let won't require transpilation.