Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript const Keyword

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/

like image 287
Kevin Meredith Avatar asked Nov 16 '13 04:11

Kevin Meredith


People also ask

What is const keyword in JavaScript?

Constants are block-scoped, much like variables declared using the let keyword. The value of a constant can't be changed through reassignment (i.e. by using the assignment operator), and it can't be redeclared (i.e. through a variable declaration).

What is the const keyword?

The const keyword specifies that a variable's value is constant and tells the compiler to prevent the programmer from modifying it.

What does const {} mean in node JS?

Const is the variables declared with the keyword const that stores constant values. const declarations are block-scoped i.e. we can access const only within the block where it was declared. const cannot be updated or re-declared i.e. const will be the same within its block and cannot be re-declare or update.

When should const be used JavaScript?

When to use JavaScript const? Always declare a variable with const when you know that the value should not be changed. Use const when you declare: A new Array.


2 Answers

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.

like image 62
Dustin Getz Avatar answered Sep 28 '22 15:09

Dustin Getz


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.

like image 22
Anthony Avatar answered Sep 28 '22 13:09

Anthony