Categories
Javascript

JavaScript const keyword

In my previous post I wrote about let keyword, in this post I would like to write about another new ECMAScript 6 keyword, const.

There is no much difference between const and let, both works similar way , both are having same level of scope.
Which means constants which are declared with const keyword are block scoped local variables which means they are limited in scope to the block, statement, or expression on which it is used.

We can not re-assign a value to constant, and you can not re-declare a constant . For example, if we have const a = 1, and then later down in the same code block, if we try to reassign const a = 2 or a = 2, JavaScript engine will throw an exception.

Using const and let, you get the same type of scoping, and just slightly different behavior with respect to reassigning/redefining values.

A constant cannot share its name with a function or a variable in the same scope.

By Arjun

I am Arjun from Hyderabad (India). I have been working as a software engineer from the last 7+ years, and it is my passion to learn new things and implement them as a practice. Aside from work, I like gardening and spending time with pets.

1 reply on “JavaScript const keyword”

Leave a Reply

Your email address will not be published. Required fields are marked *