6

In javascript, as per my understanding, we can declare variables with let and var keywords and assign it a value later.

var a;
a = 5;

But, when I try the same approach with const keyword it does not work.

const a;
Uncaught SyntaxError: Missing initializer in const declaration

I understand my question has a basic contradiction because const keywords are used for storing immutable variables. Therefore, a good coding practice may require it to be initialized while being declared.

However, I want to store global value in a const variable that is being fetched by an API. So, I was wondering if there is a way to assign a value to a pre-declared const variable in javascript?

9
  • 4
    What sense does it make? You can't reassign a const variable. If you declare it without a value, how would you give it a value later? Commented Jan 22, 2020 at 23:47
  • You may be thinking of declare stackoverflow.com/questions/49745860/… Commented Jan 22, 2020 at 23:48
  • 1
    const doesn't mean the value is immutable, it means the variable is immutable. Commented Jan 22, 2020 at 23:48
  • 1
    The proper way to solve this is to store a promise for the response in the global constant. Commented Jan 22, 2020 at 23:52
  • 2
    @Barmar Sometimes you want to set a const inside a block but want it to have a global scope. const is block scoped (just like let). If we could declare a variable to be const outside a block const a; then assign it once inside the block, it would solve the problem. The alternative would be creating a function or refactoring the code. If we could separate scope from the const property it would be nice. Commented Feb 8, 2024 at 20:20

4 Answers 4

4

const applies to the variable, not the value. You have to initialize them, because you can't assign them later -- that's the only difference they have. You can't have a variable that you can assign just once, but not reassign.

If the result is an object, you can declare the constant to hold an empty object, and then use Object.assign() to fill it in with the API response.

const result = {};
fetch(...).then(response => response.json()).then(data => Object.assign(result, data));
Sign up to request clarification or add additional context in comments.

1 Comment

Changed to "fill it in".
3

Have two ways to create a const with default value, 1 object with properties mutable, 2 array, as you can see bellow.

const a = {};
cosnt b = [];

3 Comments

And taking into consideration that array is an object ...
Yes srry for that
Nothing to be sorry about.
2

I think you can not do that way . In JavaScript const variables must be assigned a value when they are declared:

Incorrect

const PI;
PI = 3.14159265359;

Correct

const PI = 3.14159265359;

For more reference you can take a look at this which gives you a better explanation

https://www.w3schools.com/js/js_const.asp

Comments

0

You can't do so, at least not in run-time. I suggest you consider using TypeScript, which has the readonly modifier which will help you protect your variable from being overwritten during compile-time.

Comments

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.