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).

0

u/[deleted] Apr 01 '18

I can accidentally assign to a variable inside a function can't I?

Yes, this was such a common mistake to make before const.

Right?

5

u/ghostfacedcoder Apr 01 '18

Not common, no. In fact, with the way I code functions are rarely even big enough for something like that to happen.

But at the same time, all it takes is one long function (your's or your co-worker's) and one var to potentially introduce a time-consuming bug, so why not use const? Other than the two extra characters there is absolutely no downside whatsoever to using it.

As soon as you adjust to using const instead of var it just becomes a habit, and then you're protected everywhere you define a variable by habit.

→ More replies (5)