38

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.

2
  • 3
    const in JavaScript is not like final in Java - the initialization has to be part of the declaration. Commented Oct 29, 2016 at 14:09
  • 1
    If there's some initialization to be done, you may want to do it in a separate function, perhaps defined in the same scope, and use its result as the const declaration'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. Commented Oct 29, 2016 at 14:20

7 Answers 7

47

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

const foo = 1, bar = 2;
Sign up to request clarification or add additional context in comments.

Comments

21

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.

Comments

9

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.

Comments

3

You can, but is a constant so you have to assign them a value in declaration

Comments

1

const values cannot be changed after assigning. You are trying to declare it now to change its later, which is not going to work with const. The solution of this problem is that you declare it with let or var.

Comments

1

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

Comments

0

const should be assigned a value when it's declared since it makes a read-only.

1 Comment

const must be assigned a value when it's declared, anything else is invalid JavaScript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.