1

The following code build an immutable object:

Object.freeze({ "foo" : "bar" })

Is there some difference in creation using

  • var
  • let
  • const

?

sensorario
  • 20,262
  • 30
  • 97
  • 159

1 Answers1

1

The difference is that

const foo = Object.freeze({ "foo" : "bar" });

cannot be later reassigned with

foo = 'baz';

While var and let can.

Immutability is orthogonal to reassignment. There may be a need for a variable that stores immutable object to be reassigned, there may be a need to store mutable object in const.

Estus Flask
  • 206,104
  • 70
  • 425
  • 565