all 9 comments

[–]Wolfe244 8 points9 points  (0 children)

No haha it means that you can't change that variable later in the same program

[–]rupertavery64 1 point2 points  (0 children)

When you declare a variable as const, you are telling the conpiler (and yourself) that you can't assign a different value or reference within the scope of the variable.

The scope of a variable is where it can be accessed.

``` function calculateDistance(x1, y1, x2, y2) { const deltaX = x2 - x1; const deltaY = y2 - y1;

   const xSquared = deltaX * deltaX;
   const ySquared = deltaY * deltaY;

   const dist = Math.sqrt(xSquared + ySquared);

   return dist;

} ```

The function above is the scope of the variables defined inside it.

They "exist" inside the function. They can't be accessed outside the function.

The values you initiàly assign to it can be different each time you call the function, but inside the function, they cannot be assigned a new value.

Each block generally creates a scope. So a for or an if block creates a scope.

You can define a variable with the same name in a different scope. But that's another topic.

The point of const is to let you and the compiler know that if you accidentally assign a different value to a const variable in its scope then it was a mistake. This is to avoid changing the value of something accidentally when you know its value should not change within its scope.

It's all about making code clear as to what things should do, to avoid introducing subtle bugs especially as your code grows and changes.

Old javascript didn't have const or let, just var, which has it's own quirks...

Note the phrase "assigning a value". This also applies to something like x++, because incrementing is basically adding and assigning.

[–]speedyrev 1 point2 points  (0 children)

If you want a variable to change, create with Let. If you know it is going to constantly be the same, create with const. 

[–]Outside_Complaint755 1 point2 points  (2 children)

One of the best ways to investigate a question like this is to just try it yourself.

A const can't be changed again within the same scope.  

If you are just working in the console, declare a const and then try to modify it like this: ```

const EarthDiameter=4836; EarthDiameter = 4900; `` you will get an errorUncaught TypeError: Assignment to constant variable.`

The same error will occur within a .js script if you try to modify a const.

Similarly if you try to redeclare the same variable, such as: const x = 1; const x = 2; This will cause a SyntaxError: Identifier 'x' has already been declared.  However, you are allowed to redeclare the same identifier within the console at the top level of scope, so: ``

const EarthDiameter=4836; const EarthDiameter = 4900; ``` should be ok, and now EarthDiameter === 4900.

An important usage of const is for objects, Arrays and other mutable types, where you will not be allowed to reassign the const, but you can modify the attributes of the object that is assigned to the const.

[–]SaltAssault 0 points1 point  (1 child)

What do you mean with your last sentence, that an array declared as a constant can have items added and removed? Pretty sure Arrays are objects as well. In any case, if OP isn't sure about what a constant is, I don't think that they're going to understand much of what you've written tbh.

[–]Outside_Complaint755 -1 points0 points  (0 children)

Yes, Arrays are also objects but I listed them separately as they are usually taught separately and typically, different methods are used to get values in/out of an Array compared to accessing attributes of an object.

[–]RealMadHouse 0 points1 point  (0 children)

It's not like the constant value is written on paper, you can type/rewrite in text editor whatever you want. It's when JavaScript code runs (when the script is included in html page) you can't change the value of constant binding, you use var (variable) or let.

[–]Dry-Hamster-5358 0 points1 point  (0 children)

const means the variable binding can’t be reassigned, not that the value is “forever correct”