ES6: ‘const’ keyword to declare constant variables in JavaScript

In Short – “’const’ keyword is used to create a read-only reference to some value or object. Declaring a variable with const does not make it immutable but we can not redeclare the value to the assigned variable.”

In my last ES6 tutorial, I have discussed regarding ‘let’ keyword which allows us to create a block-level variable and how it is different from ‘var’. If you haven’t read about ‘let’ keyword then it is suggested to read it (Click to read about ‘let’ keyword in ES6) before going forward so as to have complete knowledge about lock-scope variables and difference between ES5 and ES6.

Properties of ‘const’ variables

  1. Variables declared using ‘const’ also have a scope of block level similar to variables declared using ‘let’.
    
    var testBlockScope = function () {
        if (true) {
            const NAME = "Codingeek";
            console.log(NAME);
        }
        console.log(NAME); // ReferenceError: NAME is not defined - because const is block scoped
    }
    
    
  2. These variables are read-only variables.
    
    var testRedeclaration = function () {
        if (true) {
            const NAME = "Codingeek";
            const NAME = "Codingeek";
            console.log(NAME); //SyntaxError: Identifier 'NAME' has already been declared
        }
    }
    
    
  3. These variables should be initialized at the time of declaration otherwise “SyntaxError: missing initialization” error is thrown.
    
    var testMissingInitialization = function () {
        if (true) {
            const NAME;
            console.log(NAME); // SyntaxError: Missing initializer in const declaration
        }
    }
    
    
  4. Declarations are not hoisted at the top i.e. accessing variable before declaring it will throw RefereceError.
    
    var testVariableHoisting = function () {
        if (true) {
            console.log(NAME); // ReferenceError: NAME is not defined
            const NAME = "Codingeek";
        }
    }
    
    
  5. These variables throw an error if defined again with the same name in the same scope(similar to ‘let’ keyword). It doesn’t matter whether the variable is declared using var, let or const variable.
    
    var testRedeclaration_SameName_SameScope = function () {
        var var1;
        let var2;
        const var3 = "test";
    
        //Below 3 declarations will throw 'SyntaxError' that the variable is already defined
        const var1 = "test";
        const var2 = "test";
        const var3 = "test";
    
        if (true) {
            // This will not throw any 'SyntaxError' as this is an enclosing scope.
            // If we use value of var1, var2 or var3 anywhere inside 'if' block then local values are used.
            const var1 = "test";
            const var2 = "test";
            const var3 = "test";
        }
    }
    
    
  6. If we declare and Object using ‘const’, it’s properties can still be changed.
    
    var testChangeObjectPropertiesForConst = function () {
        const MY_DETAILS = {name: "Codingeek"};
    
        // Updating value of exiting variable
        MY_DETAILS.name = "Codingeek.com";
        console.log(MY_DETAILS.name); // "Codingeek.com"
    
        // Adding a new value
        MY_DETAILS.niche = "Programming";
        console.log(MY_DETAILS.niche); // "Programming"
    
        console.log(MY_DETAILS); // Object {name: "Codingeek.com", niche: "Programming"}
    }
    
    
  7. When we use a const in for-of, for-in loop, it behaves similar to ‘let’ declaration except that variable’s value can’t be changed inside the loop.
    
    var testConstInsideLoops = function () {
        let details = {name: "Codingeek.com", niche: "Programming"};
    
        for (const ATTRIBUTE in details) {
            ATTRIBUTE = "NewAttribute"; //TypeError: Assignment to constant variable
            console.log(ATTRIBUTE);
        }
        console.log(ATTRIBUTE); // ReferenceError: ATTRIBUTE is not defined
    }
    
    

Thank you for reading the post.

Do share the wisdom and motivate us to keep writing even better online tutorials for free and do comment if anything is missing or wrong or you have some feedback for us.

Keep Learning. Happy Learning.. :)

  • Rajasekhar Srinivasa Velamuri

    One of the important problems that JavaScript ever dealt with were its errors…so much so that a book got even named “JavaScript – The Good Parts”. The Good Parts are like data structures. If you code using only the Good Parts, the possibility of errors is reduced drastically. If you could research on StackOverflow about the errors in JavaScript and how to tackle them then I have no doubts that your blog will be VALUABLE !

    I like giving honest opinions, your blog is good now … but it can become better !

    • hiteshgarg21

      @rajasekharsrinivasavelamuri:disqus – Thanks a lot for your valuable review. We always look for such honest feedback as they are the pillars in one’s success. i have tried my best in writing the blog.
      I would request you to do one more favor and kindly tell me what I have missed here so that I can improve it for the future reader as well as for me too.
      Thanks a lot and keep commenting.