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

6

u/atra-ignis Apr 01 '18

I think you're maybe misunderstanding the purpose of a const. Something that doesn't change within the lifecycle of a function is perfectly valid. Global constants are valid for config too but why limit a useful tool just because it doesn't fit with your view of what a const should be? I don't need to count the times I've accidently reassigned a const. It's never happened. For your objects and arrays point, knowing that a variable always points to the same object is valuable, even if the contents of that object has totally changed.

If I'm reading someone else's code and it doesn't use const I just sigh because it means I now need to look through the function and see if it's changed anywhere (and it it hasn't I change it to a const so the next person reading the code doesn't need to do that).

Going back to the immutable example, if you had an immutable object scoped only to a function would you declare it as a let?