WebMasterCampus
WEB DEVELOPER Resources

Javascript ES6 Const Keyword

Learn Javascript ES6 Const keyword


Const Keyword

The const keyword was introduced in ES6. The const keyword allows you to declare a constant(an unchangeable value). const is similar to let, except that it’s value cannot be changed.

Const Keyword Rules

  • Constant defined with const have Block Scope.
  • Constant defined with const cannot be Redeclared.
  • Constant defined with const must be Declared before use.

Const Keyword Block Scope Example

const x = 20;
console.log("x outer scope value: "+x);

{
  const x = 10;  // Const x means value cannot be changed
  console.log("x inner Scope with let : "+x);
  
  //x =15; // if you try to update with new value it will raise error.
}

//x = 30;  // if you try to update with new value it will raise error.
console.log("x outer scope value: "+x);

Const cannot be Redeclared Example

const a = 20;

const a = True; //Generate the error

console.log(a);

Const must be Declared before use


x = 30;          // const must be Declared before use
console.log(x);  // variable without declaration considered as var
Created with love and passion.