Explain Constants in Es6?

devquora
devquora

Posted On: Feb 22, 2018

 

Constants also are known as immutable variables are a special type of variables whose content is not changed. In Es6 a constant is defined using const keyword. Constants in Es6 enable protection to overwrite a variable value, improve performance and helps programmers to write readable and cleaner code.

Example

In Es6

const WEBSITE_URL = "http://www.abc.com";
WEBSITE_URL="new url"; // generate an error;
console.log(WEBSITE_URL);

In prior version of Es6

//  and only in global context and not in a block scope 

Object.defineProperty(typeof global === "object" ? global : window, "WEBSITE_URL", { 
value: "http://www.abc.com", enumerable: true, 
writable:     false, 
configurable: false 
});

console.log(WEBSITE_URL);

Also, Read Best Node JS Interview Questions

    Related Questions

    Please Login or Register to leave a response.

    Related Questions

    ES6 Interview Questions

    What is ES6?

    Es6 or ECMASCRIPT 2015 is sixth major release of ECMAScript language which comes with a lot of new features and syntax..

    ES6 Interview Questions

    List some new features of ES6

    New Features in ES6. Support for constants (also known as “immutable variables”) Block-Scope support for both varia..

    ES6 Interview Questions

    What is Babel?

    Babel is one of the most popular JavaScript transpilers and becomes the industry standard. It allows us to write ES6 cod..