8

Does the const keyword in JavaScript create an immutable reference to immutable data structures? [I'm assuming that immutable data structures exist in JavaScript.]

For string it appears to do so:

var x = "asdf";
const constantX = x;

alert("before mutation: " + constantX);
x = "mutated"
alert("after mutation: " + constantX);

output:

before mutation: asdf

after mutation: asdf

http://jsfiddle.net/hVJ2a/

Community
  • 1
  • 1
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384

8 Answers8

17

first you aren't mutating the string, you're reassigning the reference.

You're right that const isn't available in all common browsers. But even if it were, it is not sufficient. const will prevent the reference from being reassigned - but if you have a const reference to a mutable object, you haven't accomplished very much.

const var o = { foo: 'bar' };
o = { foo: 'baz'}; // throws
o.foo = 'baz'; // allowed

So that brings us to your question, does js even have immutable data structures? No, js does not come with them. Immutable datastructures can be coded as a library - and assignment is not even defined, so o.foo = 'baz' doesn't even make sense. You have to write it as const var o2 = o.assoc('foo', 'baz') which will return a brand new object o2 instead of mutating o

But immutable data structures as a library doesn't mean much if nobody uses them. E.g. if angular uses regular javascript datastructures, you have to use them too. Otherwise you'd have to convert between mutable and immutable at the boundary between your code and angular.

opinion follows:

IMO your only practical options for doing real functional programming for production browser apps is to wait around for something like ClojureScript to mature. ClojureScript reboots the library ecosystem, so as libraries get written, they use immutable datastructures by default.

You can of course do half-baked functional programming in javascript using tools like underscore.js and Facebook React, which is what I do for the production webapps I build. But you have to establish immutability by convention, and people are going to mutate things on accident or because they don't know any better and you have to deal with those challenges.

Dustin Getz
  • 21,282
  • 15
  • 82
  • 131
8

Wrong. It is not immutable, from the MDN Documentation for const:

The const declaration creates a read-only reference to a value. It does not mean the value it holds is immutable, just that the variable identifier cannot be reassigned.

Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
Anthony
  • 13,434
  • 14
  • 60
  • 80
  • 1
    Well, yes, it creates a reference to mutable data, but the reference itself is immutable, by definition. So, the answer to the question would be "it creates an immutable reference to mutable data structures". – Agamemnus Aug 30 '18 at 23:12
7

Yes, it does create an immutable reference, but it is not been standardized and is not supported in all browsers.

See this MDN article on the const keyword for details and compatability.

However, it doesn't do anything to set the referenced data structure as immutable. Several answers below address that question using, for example, Object.freeze

Robbie Wxyz
  • 7,671
  • 2
  • 32
  • 47
7

The only immutable data structure (something that is allocated on heap) is string. All other object alike things like objects, arrays and functions are mutable by default.

The const creates immutable variable. Yes, "const variable" sounds controversial but that is closest term I can come up with for JS.

The const content cannot be changed outside of its declaration. But it may contain reference, for example, to the object that is mutable by itself. So you can modify its properties through that const reference to it.

And if you want to make object to be immutable then there is a Object.freeze(obj) method.

c-smile
  • 26,734
  • 7
  • 59
  • 86
5

You can create CONST like values using ES5 Object.defineProperty. The crummy part is that it must be bound to an object

CONSTS = {};
Object.defineProperty(CONSTS, 'FOO', {
    value: 'bar'
});

CONSTS.FOO = 'derp' // Will throw error in strict mode
delete CONSTS.FOO // will throw error in strict mode
Trevor
  • 11,269
  • 2
  • 33
  • 40
1

Yes that is right. A const would only declare a read-only named constant. Changing its value will have no effect.

Reference for const on MDN:

Const creates a constant that can be global or local to the function in which it is declared. Constants follow the same scope rules as variables.

The value of a constant cannot change through re-assignment, and a constant cannot be re-declared. Because of this, although it is possible to declare a constant without initializing it, it would be useless to do so.

A constant cannot share its name with a function or a variable in the same scope.

Here's the browser compatibility matrix.

Community
  • 1
  • 1
Nick
  • 4,002
  • 4
  • 30
  • 41
0

const is a proposed feature of ECMAScript(together with a properly block-scoped let it is supposed to replace var and implicit global). ECMAScript Harmony is a grab-bag of ideas for the next versions of ECMAScript.

If you looking for read only variable, you can do like this

var constants = new (function() {
  var x = 200;
  this.getX = function() { return x; };
 })();

You can use like this

constants.getX()
Prateek
  • 6,785
  • 2
  • 24
  • 37
0

For example:

const basket = [1,2,3];

//Even though the variable is declared as const this one works
basket[0] = 1111;
console.log(basket);

//This one throws an error
basket = "Other primitive type."
serkan
  • 6,885
  • 4
  • 41
  • 49