Let vs Var | Javascript Basics

Do we really need ‘Let’ if we already have ‘Var’?

Rajul Singh
The Startup
4 min readSep 10, 2020

--

I know I’m not good at photo editing 😐

What is Var?

Let see what MDN says:

The var statement declares a function-scoped or globally-scoped variable, optionally initializing it to a value.

Scope? What is it?

Scope determines the accessibility of variables or other expressions. To know in detail about scope check this link.

Var is Global-scoped or Function-scoped

Global-scoped means if a variable is declared outside a function, it is available for all functions and scripts on the web page or, in other words, it is available for the whole window.

Function-scoped means if a variable is declared inside a function then it is available & accessible within the function only.

let see an example:

As variable num is outside any function, it has global scope. while variable str is inside the function so it has function scope. which means it can not be accessed outside greeting. Try this in your editor .

Most of us know this about Var so the question arises that what's the real issue with var?

Var can be updated and Re-declared. i.e,

This is allowed in javascript and come on we have updated variables like this a lot of times. where’s the issue?

This is also allowed in the same scope. This is the issue. Now imagine you or someone has already declared num with some value at other parts of the code (but within the same scope), if you redeclare num unknowingly, it will get redefined and unexpected bugs can pop up. let’s see this example

‘LET’ saves us

But before diving deep into ‘Let’, let see what MDN says:

The let statement declares a block-scoped local variable, optionally initializing it to a value.

Let is Block-scoped which means a variable declared with let inside a block is accessible only within that block.

I know what you are thinking. what is a block in the first place?

Block is a code area bounded by curly braces {}. Anything within {} represents a block. we generally encounter block with some conditionals like if statements or loops but it is allowed to use it standalone too.

This is a completely valid block. In terms of block-scoping of let, check this example

but in the case of Var:

As let is block-scoped, variable b is not accessible outside the scope it is declared in. but in the case of Var which is global or function-scoped, b is being accessed within the scoped it is defined in. hence no error.

Let can be updated but can’t be re-declared

Variables declared with let can be updated within its scope. i.e,

but Re-declaration is not allowed

As num is declared in the same scope twice, it throws an error (var does not show error in the same case, as discussed above). but note that the same variable can be defined in different scopes without an error which means

We have covered a lot about Var and Let. but there is one thing left which is

Hoisting of Var & Let

Javascript moves variables and functions declarations to the top of their scope before execution. this is called hoisting. Example:

Both Var and Let variables are moved to the top but variables with var are initialized with undefined value while the Let variable is not initialized.

That’s all for Var vs Let. As it is quite clear that Let is the Smart choice nowadays.

I’ll publish more such blogs. Thanks for reading 😄.

--

--