EcmaScript2017

Modern Internet Magazine About IT Industry

VARIABLE DECLARATIONS

One of the biggest sources of bugs and confusion in JavaScript has been variable scoping. Without var everything was global, with var it was better but still weird. ES6 brings us let and const with proper lexical scoping, which makes it easier to understand where a variable is and isn’t defined.

Constants follow the same scoping rules, but their value cannot be reassigned. const protects you against accidentally changing variables. It’s good to use it for everything by default.

Careful, though. Constants are tricky with arrays and objects. The reference becomes constant, but the value does not.

One of my favorite features present in most modern JavaScript codebases is omitting repetitive object keys. I don’t think this is quite in the standard yet, but it works in most React-based codebases via Babel. I include it because it’s commonly used in the wild.