ES6 Tutorials  : Using const keyword

ES6 Tutorials : Using const keyword

  • 2016-08-29
  • 1466

How to declare Constants(const) in ES6?

The const _keyword makes any deceleration a read-only. In other words the variable reference cannot be reassigned. It means that the value it holds is mutable. Let’s first see the way in which this is used. In the sample below we show the ways in which it can be used similarly as _let.

How to use Const keyword

/*Deceleration one at a time*/
const a = 10;
const b = 20;
const c = 30;
/*Multiple variable Deceleration*/
const a = 10,
  b = 20,
  c = 30;

The above mentioned code is the most simple way in which constants can be used.

A constant can be used to declare either a global or a local variable local to a function. It’s also mandatory that constants should be pre-initialized at the time of deceleration itself. You must note reassignment of such variables leads to errors.

Since **const ** is a block scoped keyword just like let, any reassignment is not valid and inside each block it creates a new variable that is being referred. This makes the usage of const scope specific.

This was a the complete reference to this keyword. Hope using this would be easy and won’t be a problem in future. Using the keyword would help you to declare many variables which do not change over time. for example consider a simple math library. These kind of libraries heavily depends on declaring constants. Many other applications, even yours depends completely on the various constants that play a major role in the framework. These as well fit into the scenario.


ES6 Javascript: The Complete Developer’s Guide

JavaScript Promises: Applications in ES6 and AngularJS

Learning ECMAScript 6: Moving to the New JavaScript

ES6 Javascript Essentials (With Exercise Files)

ES6 Bootcamp - Next Generation JavaScript