The following code build an immutable object:
Object.freeze({ "foo" : "bar" })
Is there some difference in creation using
- var
- let
- const
?
The following code build an immutable object:
Object.freeze({ "foo" : "bar" })
Is there some difference in creation using
?
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
.