JavaScript | Const

ES2015 (ES6) introduced const keyword to define a new variable. The difference in const variable declaration than others is that it cannot be reassigned.

Properties:

  • Cannot be reassigned.
  • Block Scope
  • It can be assign on the variable on declaration line.
  • Primitive value.
  • The property of a const object can be change but it cannot be change to reference to the new object
  • The values inside the const array can be change, it can add new items to const arrays but it cannot reference to a new array.
  • Re-declaring of a const variable inside different block scope is allowed.
  • Cannot be Hoisted.
  • Create only read only reference to value.

Example 1: It describes that the const variable cannot be reassigned.



filter_none

edit
close

play_arrow

link
brightness_4
code

<script type="text/javascript">
    const x = 12;
    x = 13;
    x += 1;
</script>                    

chevron_right


Output:

Uncaught TypeError: Assignment to constant variable.

Example 2: It describes the const variable which contains the Block Scope.

filter_none

edit
close

play_arrow

link
brightness_4
code

<script type="text/javascript">
    const x = 22;
    {
        const x = 90;
        console.log(x);
   
        {
            const x = 77;
            console.log(x);
        }
        {
            const x = 45;
            console.log(x);
        }
    }
    console.log(x);
</script>

chevron_right


Output:

90
77
45
22

Example 3: It describes the const variable and assigned it after declaration.

filter_none

edit
close

play_arrow

link
brightness_4
code

<script type="text/javascript">
    const x;
    x = 12;
</script>

chevron_right


Output:

Uncaught SyntaxError: Missing initializer in const declaration

Example 4: It describes the const variable cannot be Hoisted.

filter_none

edit
close

play_arrow

link
brightness_4
code

<script type="text/javascript">
    x = 3;
    console.log(x);
    const x;
</script>

chevron_right


Output:

Uncaught SyntaxError: Missing initializer in const declaration

Example 5: It describes that the array values can be modified only reference to array cannot be change.

filter_none

edit
close

play_arrow

link
brightness_4
code

<script type="text/javascript">
  
    // Changing the content of array is
    // possible in cost array
    const arr1 = ["pankaj", "sumit", "chandan", "ajay"];
      
    console.log(arr1.toString());
      
    arr1[2] = "Narayan"; // possible
      
    console.log(arr1.toString());
</script>

chevron_right


Output:

pankaj, sumit, chandan, ajay
pankaj, sumit, Narayan, ajay

Example 6: It describes that the object properties can be modified only reference to object cannot be changed.

filter_none

edit
close

play_arrow

link
brightness_4
code

<script type="text/javascript">
  
    const person = {
        first_name: "Pankaj",
        last_name: "Singh",
        Age: 20,
        About: "Web Developer and Competitive Programmer"
    };
      
    console.log(person);
      
    // It is possible
    person.first_name = "Aryan";
    person.last_name = "Yadav";
    person.Age = 22;
    person.About = "Commerce undergraduate";
      
    console.log(person);
    
    // it is not possible
    // const person={
    //     "first_name":"Aryan",
    //     "last_name":"Yadav",
    //     "Age":22,
    //     "About":"Commerce undergraduate"
    // }
</script>

chevron_right


Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.