JavaScript ES6 - Let, Var, Const ?

Hardeep Singh
3 min readSep 17, 2017

--

ES6 (Ecmascript), it was created to standardize JavaScript, so as to encourage the development of multiple independent implementations. Basically, it’s just providing us new cool features to use JavaScript to make developer life easier. Now the latest version is Ecmascript6 and Ecmascript7 has been launched but this is not exactly compatible with all Browsers. One of the feature of Ecmascript6 is how we can define the type of variable or we can say new data types.

So, all three data types i.e var, let, const can be use for defining any thing like number, string, object , arrays. When I saw this new thing and having a question myself that when to use what ?

Here is the difference -

VAR -

It is use for defining the anything all over the JS file and can be override successfully even on New declaration with same name. Let see with example-

and this same theory is also applicable when i use the Lexical Scope i.e { } or you can say Blocked Scope with { }. Let See -

So, here we can see var data type is just used all-over the JS file.

LET -

It is also use for defining anything all over the JS file what makes let different from var -

  • I can not define same let variable if that second variable not in any Lexical Scope i.e { } or Blocked Scope with { }. So I can not define same name of let datatype in same Scope, it gives me error i.e - Identifier ‘text’ has already been declared.
  • But when it is define in the Scope { }, they behave like new variable but only for that particular scope in which that is define. Let see with example-

So, let plays main role in Lexical Scope.

Const -

It is also use for defining the things which you know they will be CONSTANT all over the JS file so they can’t be override, that’s why they are Constant. Here is the example -

But Here is the Twist Lets See an example -

You will be like What the hell :0 ? Constant can not override.

Yes you are right but only if the constant variable is not in type of reference i.e Object and Arrays stores Reference so they can be change. Here is the example of Array -

You can try these examples on your machine to make it more understand. I think now you can see difference between Var, Let, Const.

Hope you like it.

Cheers.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Hardeep Singh
Hardeep Singh

Written by Hardeep Singh

Software Development Engineer 3 | ReactJs | NodeJs | ExpressJs | NextJs | MongoDB | Micro Frontend | Micro Services

No responses yet

Write a response