Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I declare multiple const in JavaScript?

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.

like image 769
knitevision Avatar asked Oct 29 '16 14:10

knitevision


People also ask

Can I declare multiple variables JavaScript?

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.

What is const {} in JS?

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.

How do you declare constants in JavaScript?

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.

How do you declare more than one variable?

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.


1 Answers

Because a const declaration has to be initialized as well. This will work:

const foo = 1, bar = 2; 
like image 177
Sean Sobey Avatar answered Sep 22 '22 06:09

Sean Sobey