1

The following code build an immutable object:

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

Is there some difference in creation using

  • var
  • let
  • const

?

sensorario
  • 18,131
  • 26
  • 91
  • 148

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
  • 179,509
  • 61
  • 360
  • 499