This is correct syntax:
let foo, bar;
This is incorrect
const foo, bar;
Why is that?
And is there a way to declare a number of constants in one place, and define them in another? Apart from bound declaration & definition.
Although JavaScript allows to declare multiple variables in a single chain, it is recommended to declare each variable separately as it is easier to read and reduce the risk of unexpected behaviors in some cases.
The const declaration creates a read-only reference to a value. It does not mean the value it holds is immutable—just that the variable identifier cannot be reassigned. For instance, in the case where the content is an object, this means the object's contents (e.g., its properties) can be altered.
ES6 provides a new way of declaring a constant by using the const keyword. The const keyword creates a read-only reference to a value. By convention, the constant identifiers are in uppercase. Like the let keyword, the const keyword declares blocked-scope variables.
When more than one variable is declared in a single declaration, ensure that both the type and the initial value of each variable are self-evident. Such declarations are not required to be on a separate line, and the explanatory comment may be omitted.
Because a const declaration has to be initialized as well. This will work:
const foo = 1, bar = 2;
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