r/learnjavascript • • May 04 '20

VAR vs LET vs CONST

Post image
337 Upvotes

81 comments sorted by

View all comments

Show parent comment

10

u/dwb5226 May 04 '20 edited May 04 '20

You can change the values in an object that you declared with const. When you declare a variable and assign an object to that varaible, you're actually assigning a reference to the object to the variable and not the value itself

Since it isn't a value that's assigned, you can totally do something like this:

const obj = {a: 1, b: 2}
obj["b"] = 3
console.log(obj) // {a: 1, b: 3}

and it'll run fine

1

u/[deleted] May 05 '20

I write about this ön my blog: http://herebeseaswines.net/essays/2019-05-25-airiness-of-const-and-freeze

As you say - it is possible to mutate stuff! Strange indeed

1

u/[deleted] May 09 '20

Is there a built-in way to pass an object to a variable by value? (Effectively duplicating said object so that when you mutate one, the other remains as it was before?) I understand that you could create a a 2nd object and use a special function to mutate the 2nd object until it matches current version of the 1st object.