In JavaScript, `const` means that the identifier can't be reassigned. (Not to be confused with immutable values. Unlike true immutable datatypes such as those produced by Immutable.
As a general rule, you should always declare variables with const, if you realize that the value of the variable needs to change, go back and change it to let. Use let when you know that the value of a variable will change. Use const for every other variable.
The property of a const object can be change but it cannot be change to reference to the new object.
We are considering adopting the Google JavaScript coding guidelines within our company to maintain consistency across projects but one thing is confusing me. In the section on constants it says to use the @const
keyword annotation for compile-time constant enforcement but I have never come across the @
symbol before. Is this a Google extension or part of the core language?
Here is the full text:
For non-primitives, use the
@const
annotation.
/** * The number of seconds in each of the given units. * @type {Object.<number>} * @const */ goog.example.SECONDS_TABLE = { minute: 60, hour: 60 * 60 day: 60 * 60 * 24 }
This allows the compiler to enforce constant-ness.
As for the
const
keyword, Internet Explorer doesn't parse it, so don't use it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With