Kevin Meredith Kevin Meredith - 3 years ago 156
Javascript Question

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/

Answer Source

Yes, it does, 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download