const in Javascript - ES6

Code for this and several other ES-6 features can be found in my github


"const" keyword in ES6 is used to declare immutable variables. This is not something new to ES6. It was completely possible to declare immutable variables in ES5. But it had to be done in a really verbose manner by using the object.defineProperty keyword and setting thewritable property to null as follows:

Object.defineProperty(typeof global === "object" ? global : window,'immutable5',{
configurable: false,
enumerable: false,
writable: false,
value:'I am immutable by ES5'});
However in ES-6 immutable variables can be defined in a simple manner as follows:

const immutable6 = 'I am immutable by ES6'; 

Comments

Popular posts from this blog

Docker Commands

Dockerfile

ES-6 classes