CONSTANT changes, even though I don't change it

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By edwood

I define a constant and a variable. I assign the constant to the variable and manipulate the variable but I don’t know why the constant also changes.

Code:
const DEF = [[0,2],
[1,1],
[2,2],
[3,3]]
var deflist
deflist[0][1] *= 1.5

Then the output shows that DEF[0][1] also changes to 3.
It is weird that a constant is not constant even I have done nothing on it.
Does anyone know what happens?

:bust_in_silhouette: Reply From: Dlean Jeans

You need to duplicate with deep=true:

var deflist = DEF.duplicate(true)
deflist[0][2] *= 1.5

Click here to run the code above.

Seems like const doesn’t keep arrays from being changed.

Oh, I see. Let me try. Thx XD

edwood | 2019-06-23 10:14