3
   

Declaring an object as const makes it impossible to change that object. true or false?

 
 
averma7
 
Reply Sun 24 Sep, 2017 08:30 pm
Declaring an object as const makes it impossible to change that object. true or false?
  • Topic Stats
  • Top Replies
  • Link to this Topic
Type: Question • Score: 3 • Views: 816 • Replies: 5
No top replies

 
fresco
 
  1  
Reply Mon 25 Sep, 2017 12:54 am
@averma7,
Give an example. Physical objects change chemically all the time. Do you mean functionally constant?
roger
 
  1  
Reply Mon 25 Sep, 2017 01:08 am
@fresco,
I'm considering the possibility of a constant in a mathematical statement. Not sure, though.
centrox
 
  1  
Reply Mon 25 Sep, 2017 02:27 am
@averma7,
Once declared, the value of a JavaScript constant cannot be changed. Check a dictionary for the meaning of the world "constant".
0 Replies
 
fresco
 
  1  
Reply Mon 25 Sep, 2017 02:48 am
@roger,
Yes. Come to think of it, the abbreviation 'const' implies mathematics or commputer language as centrox has just pointed out.
0 Replies
 
maxdancona
 
  2  
Reply Mon 25 Sep, 2017 02:54 am
@averma7,
averma7 wrote:

Declaring an object as const makes it impossible to change that object. true or false?


First of all. This is a computer programming question. Philosophy is meaningless (at least in this context Wink ).

The answer is "No". This is a poorly worded question, but I am pretty sure that they are asking this to see if you understand that const does not make an object immutable.

Consider the following code (and you can easily try this in JSFiddle):

Code:
var foo = { one : "red", two : "blue", three : "green"};
const bar = foo;

foo.one = "yellow"; // legal (no error). You are changing the object
bar = {animal: "dog"}; // will cause error. You are making an assignment.


The const keyword will prevent you from assigning to the variable. It will not prevent you from changing the object that has already been assigned.

Quote:
The const declaration creates a read-only reference to a value. It does not mean the value it holds is immutable, just that the variable identifier cannot be reassigned. For instance, in the case where the content is an object, this means the object's contents (e.g., its parameters) can be altered.


https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const

0 Replies
 
 

Related Topics

 
  1. Forums
  2. » Declaring an object as const makes it impossible to change that object. true or false?
Copyright © 2019 MadLab, LLC :: Terms of Service :: Privacy Policy :: Page generated in 0.03 seconds on 07/21/2019 at 11:02:08