QNimate

  • CoursesVideos
  • WP PremiumPlugins
  • DemosLab
  • Home
  • QIdea
  • QTrack
Home Carbon Ads JavaScript “const” Keyword

JavaScript “const” Keyword

This post is a part 2 of ECMAScript 6 Complete Tutorial post series.

“const” keyword was introduced in ES6. It lets you define read only variables using JavaScript. Variables created using “const” are block scoped(or bracket scoped). Redeclaring a “const” variable in the same scope throws an error.

Here is an code example:

const x = 12;

//an constant 'x' is already available in this scope therefore the below line throws an error when you are try to create a new x variable.
const x = 13;

if(true)
{
    //an constant 'x' is available in this scope but not defined in this scope therefore the below line will not throw error instead define a new "x" inside this scope.
    const x = 13;
   
    //here 'y' is available inside this scope not outside this scope
    const y = 11;
}

//here creating a new 'y' will not throw an error because no other 'y' is available in this scope(i.e., global scope)
const y = 12;

Just remember that in a scope you cannot redeclare or change value of an “const” variable if a variable with same name is already available for access in that scope.

Feb 13, 2015Narayan Prusty
JavaScript "let" KeywordJavaScript Function Return Multiple Values
Comments: 4
  1. pachaiappan
    5 years ago

    narayan code is correct,because Hasib will created a new x…
    const X = 12;
    if (true) {
    X = 10;
    console.log(X);
    }
    console.log(X);

    then
    output is ::ecma.js:51 Uncaught TypeError: Assignment to constant variable.
    pls check its correct…

    ReplyCancel
  2. Juliet
    6 years ago

    Great Job…

    ReplyCancel
  3. Md. Hasib Hasan Tarafder
    7 years ago

    Hi Narayan,

    In your code example, you mention that const x = 13; under the if block gives an error because it is already available in this scope.

    But, as per my concern const keyword is another thing that is let you creating a block scoped variable like let keyword. Only difference is it is read-only.

    Following code snippet run smoothly without giving error and it results 10 & 12. Please have a look

    'use strict';
    const X = 12;
    if (true) {
    const X = 10;
    console.log(X);
    }
    console.log(X);

    Please let me correct if i am wrong.

    Thanks

    ReplyCancel
    • Narayan Prusty
      7 years ago

      You’re right. I actually put up an wrong example. I have changed it. Kindly see the new example.

      Thanks.

      ReplyCancel

Leave a Reply to Narayan Prusty Cancel reply

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

Narayan Prusty

I am a software engineer specialising in Blockchain, DevOps and Go/JavaScript. This is my personal blog where I write about things that I learn and feel interesting to share.

7 years ago 4 Comments Web Development
Share this
0
GooglePlus
0
Facebook
0
Twitter
0
Linkedin
Related Articles
  • JavaScript “let” Keyword
  • JavaScript “class” Keyword
  • JavaScript Function Techniques You May Not Know
  • JavaScript Global Variables And Memory Leakage
  • How To Detect Adblock using JavaScript
Our Sponsor
My Books

2014 - 2015 © QNimate
All tutorials MIT license