Home > UI/UX Development > JavaScript > Ecmascript 6: Scoping – Block scoped variable

Ecmascript 6: Scoping – Block scoped variable

Javascript followed function level and global scope for a very long time which created a lot of problems. Developers could declare a variable anywhere as global and variables lived even outside loops and if else blocks because of the function level scope.

Ecmascript 6 came up with support for block level scoping using 2 new keywords to define a variable. var still is supported along with const and let.

const: It is used to define a constant whose value cannot be changed nor it can be re-declared in the block scope where it is defined. Run the following code.

Add the following line after the last alert statement now:

Note: if you declare an Object constant then its values can be changed using the dot(.) operator.

let: It is used to define a variable whose value can be changed but it cannot be re-declared in the block scope where it is defined. Run the following code.

Browser Support: Ecmascript 6 is supported by all modern browsers. The above mentioned features are compatible from IE 11+ and Edge, Android browser for version 6.0 and above and iOS version 10 and above.

This Article is TAGGED in , , . BOOKMARK THE permalink.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">