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.
A normal variable (declared with var or let) can be declared without a value, since you can assign to them later:
let foo; // foo has value undefined
foo = 3; // foo has value 3
However, you can't do the same with constants since you can't change their value after they have been declared:
const foo; // ...?
foo = 3; // this is not allowed because foo is constant
Therefore, you must declare and assign a value to a constant at the same time. This can be done with multiple consts at once:
const foo = 3,
bar = 8; // this works fine, foo and bar are both constants
There's no way to declare a const in one spot and assign it in another. You must use var or let instead.
As Sean Sobey said in his answer, this is because const values have to be declared with values.
I would add that you can declare multiple const values in one statement, e.g.
const [xMin, xMax, yMin, yMax] = [-2, 2, -1.5, 1.5];
It is useful to note (contrary to other answers here) that a const is not immutable. See:
https://softwareengineering.stackexchange.com/questions/149555/difference-between-immutable-and-const
For example:
const item = { price: 10 };
item.price= 15;
console.log(item.price);
This will output the updated price of 15.
I would declare multiple variables like this:
const latCoordinate = 12, longCoordinate = 34
Or you could also do something like that if you have an Object in the first place:
const {latCoord, longCoord} = {latCoord: -0.12768, longCoord: 51.50354}
In that case, you can name variables the way you want like so:
const {latCoord: lat, longCoord: lng} = {latCoord: -0.12768, longCoord: 51.50354}
console.log(lat, lng) // => -0.12768, 51.50354
const should be assigned a value when it's declared since it makes a read-only.
const must be assigned a value when it's declared, anything else is invalid JavaScript.
constin JavaScript is not likefinalin Java - the initialization has to be part of the declaration.constdeclaration's assignment. Also, there should be no need to pre-declare these at the top of a scope if that was the intent; declare them where you need them.