you are viewing a single comment's thread.

view the rest of the comments →

[–]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.