3

I've heard similar questions, but not the answer that I wanted; I do not count const because: 1). it doesn't actually make it immutable, it only makes the reference immutable 2). it messes with the scope, and I want it to work outside the block, too 3). not all browsers support it yet

 {
 const hello = ["hello", "world"];
 hello.push("!!!");
 console.log(hello);//outputs "hello", "world", "!!!"
 }
 //and it doesn't, and shouldn't, work here
 console.log(hello);
Martijn Pieters
1.1m324 gold badges4.2k silver badges3.4k bronze badges
asked Jul 23, 2019 at 20:14
2

3 Answers 3

4

Just use Object.freeze

const immutableArray = Object.freeze([1,2,4])
answered Jul 23, 2019 at 20:19

4 Comments

thanks, but that only works for objects! how can it work for any data type?
const name = Object.freeze(<anything>) works just fine. <anything> can be an Array, an Object, a Number, String, Boolean or anything else. Try it in your browser.
Yeap. Sorry I missed your comment. But yes, Object.freeze accepts any reference type. Remember that almost everything is an object in js (except primitives)
@Dupocas Aren't Arrays, Strings, Booleans, Numbers, etc, primitives? nevertheless, it does indeed work on anything, it would just be pointless, now that I think about it: Object.seal(4) for e.g.
2

You can use Object.freeze for this (obviously only on objects).

const hello = Object.freeze(["hello", "world"]);
// hello.push("!!!");
// will throw "TypeError: can't define array index property past the end of an array with non-writable length"
// hello.length = 0;
// will fail silently
// hello.reverse();
// will throw "TypeError: 0 is read-only"
// hello[0] = "peter";
// will fail silently

From MDN:

The Object.freeze() method freezes an object. A frozen object can no longer be changed; freezing an object prevents new properties from being added to it, existing properties from being removed, prevents changing the enumerability, configurability, or writability of existing properties, and prevents the values of existing properties from being changed. In addition, freezing an object also prevents its prototype from being changed. freeze() returns the same object that was passed in.

However, there is no keyword to define a completely immutable variable without using Object.freeze or Object.seal on the variable's value.

For a less restrictive approach Javascript also has Object.seal().

Sapphire_Brick
1,69417 silver badges28 bronze badges
answered Jul 23, 2019 at 20:21

Comments

1

The way to do it without const is to use Object.defineProperty, and like I wanted, it behaves like var in terms of scope:

{
 Object.defineProperty(typeof global === "object" ? global : window, "PI", {
 value: Object.seal(3.141593),
 enumerable: true,
 writable: false,
 configurable: false
 });
}
console.log(PI); // 3.141593

The only problem is that it that it doesn't throw an error outside of strict mode.

answered Nov 11, 2019 at 16:30

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.