Javascript let, const and var

in #utopian-io9 years ago (edited)

javascript-736401_640.png

Javascript is the lingua franca for the front end of web development. In modern JS world (ES6 onwards), there had introduced let and const for variable declaration. A lot of newbies to ES6 will be confused on what are the difference of them with var?

In this tutorial, I am going to explain which case of scenario to use which type of declaration.

Javascript Primitive Value

Primitive Value is a data type aren't object and has no method. Primitive Value are immutable by default, unless you use a wrapper. (e.g. var a = new String('hello'))

Currently, there are 6 types of primitive value is JS:-

  • null
  • undefined
  • boolean
  • number
  • string
  • symbol (ES6)

Everything else extends from Object (where object we deal with a lot of mutable object)

Observe the difference between object and primitive value

Screen Shot 2017-12-28 at 10.13.30 PM.png

ObjectPrimitive Value
Object can be mutatedPrimitive Value are immutable
var obj = new String("hello")var prim = "hello"
Seldom being directly usePreferred way of declaration

Variables Declaration & Scope

In modern JavaScript (EcmaScript6) it introduce 2 new variable declaration which is let and const, there is 3 types of variables declaration and scope:-

  • var
  • let
  • const
varletconst
declarations are hoisteddeclaration are not hoisteddeclaration are not hoisted
not block-scopeblock-scopeblock-scope
Reassignment is allowedReassignment is allowedReassignment is not allowed
--must be initialize

hoisted means that the variable can be used before it declare.

Hoisted Example

x = 5;
console.log(x);
var x;

This code will work because var is able to be hoiste. But this does not work with let or const because both methods does not support hoiste.

Block Scope

var

var x = 3;
if (x===3) {
  var y = 5;
}
console.log(x+y); // 8

This code will return value of 8 because var is function scope instead of block scope.

let or const

let x = 3;
if (x===3) {
  let y = 5;
}
console.log(x+y); // ReferenceError: y is not defined

You will get a reference error.

Use Case

  • Use const whenever it is possible.
  • const means that the value does not change.
  • In 90% of the time, we should be using let and const. Unless, we are dealing with hoisted situation. (example shown below)

Using let

x = 5;
console.log(x);
let x;

Result:
Screen Shot 2017-12-28 at 10.41.41 PM.png

Using var

x = 5;
console.log(x);
var x;

Result:
Screen Shot 2017-12-28 at 10.40.18 PM.png

This is because let is not hoisted. Therefore, for this certain scenario, var is the one that we should be using.

Source

Angus Croll, The Secret Life of JavaScript Primitives, September 27, 2010

About Me

I am Lai Weng Han (Johnson), you can find me on Twitter.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by superoo7 from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews/crimsonclad, and netuoso. The goal is to help Steemit grow by supporting Minnows and creating a social network. Please find us in the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP. Be sure to leave at least 50SP undelegated on your account.

Your contribution cannot be approved yet because it is not as informative as other contributions. See the Utopian Rules. Please edit your contribution and add try to improve the length and detail of your contribution (or add more images/mockups/screenshots), to reapply for approval.

You may edit your post here, as shown below:

You can contact us on Discord.
[utopian-moderator]

@shreyasgune Just done. Thank you.

Your contribution cannot be approved because it does not follow the Utopian Rules.

You can contact us on Discord.
[utopian-moderator]

The @OriginalWorks bot has determined this post by @superoo7 to be original material and upvoted it!

ezgif.com-resize.gif

To call @OriginalWorks, simply reply to any post with @originalworks or !originalworks in your message!

Coin Marketplace

STEEM 0.04
TRX 0.33
JST 0.101
BTC 65974.96
ETH 1925.79
USDT 1.00
SBD 0.38