Enough JavaScript to get you Started : #16 var vs let vs const

whoadarshpandya

Adarsh Pandya

Posted on January 22, 2021

Enough JavaScript to get you Started : #16 var vs let vs const

Before we start

πŸ‘‰ Before we start this article i would like to clarify some technical jargons for you

πŸ‘‰ Scope : Scope is nothing but a code block where the variable is accessible for usage

πŸ‘‰ Global Scope : Global Scope means variable is declared globally (not in some condition or function) hence it can be used any where throughout the execution of program

πŸ‘‰ Local/ Functional Scope : this simply means that when we declare a variable on function level or somewhere locally in code block then it's not accessible outside of that particular scope (imagine variables declared in functions , loops ,conditionals...)

πŸ‘‰ Block Scope : Blocks are nothing but piece of code written inside any curly braces {...} [ex. if block , or function's block]

Var

πŸ‘‰ Var is the oldest way of declaring variable

πŸ‘‰ Var can be globally and functionally scoped

πŸ‘‰ If we Declare var inside function it becomes functionally scoped if we declare it outside function it becomes globally scoped and is available anywhere in program

πŸ‘‰ can be redeclared or updated

πŸ‘‰Example of Scope

var a = 10; // global variable

function fun()
{
    // functional scoped variable
    var b = 20;
    console.log(a);
    console.log(b);
}
fun();
console.log(a);
console.log(b);
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ output

-> inside function
10 βœ”
20 βœ”
-> outside function
10 βœ”
uncaught reference : b is not defined ❌ 
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Notice that functions can access both global and functional variables.

πŸ‘‰ Example of Re-declaration

// variables with var can be re-decalred βœ”
var a = 10;
var a = 20;
// you won't get any error
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Example of updatable variables

// variables with var can be updated βœ”
var a =10;
a=20;
// you won't get any error
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Problems with var

πŸ‘‰ Redefining variables won't throw any errors which means it's tricky to remember which variable is already there and which variable is new.


Let

πŸ‘‰ Let is the modern way of declaring variables introduces in ES2015

πŸ‘‰ Let is now recommended way of declaring variables

πŸ‘‰ Let is block scoped

πŸ‘‰ Let can be updated but not redeclared

πŸ‘‰Example of Declaration

// let can be updated βœ”
let a = 30; βœ”
a = 40; βœ”
// but not redeclared ❌
let b = 40; βœ”
let b = 90 ;❌

// error : Identifier 'b' has already been declared
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰Example of block scope

let sayHi = "hi";
if(sayHi === "hi")
{
    let newMsg = "how are you?";
   console.log(sayHi); // outputs "hi"
}

console.log(sayHi); βœ”
console.log(newMsg); ❌ // 'newMsg is not defined'
Enter fullscreen mode Exit fullscreen mode

Const

πŸ‘‰ Variables declared with const remains same throughout the execution

πŸ‘‰ variables declared with const are not redeclarable or updatable

πŸ‘‰ if const variables declared outside any block they becomes global scoped , but if they are declared within block they becomes block scoped

πŸ‘‰ values can not be changes or new values can not be assigned to const variables

πŸ‘‰ Example:

const sayHi = "hi";
const sayHi = "hello"; ❌ // will throw error

//and ...

const msg = "buy bread from market";
msg = "new msg here"; ❌ // error:  assignment to constant variable.
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ But...

const numbers = [1,2,3];
number[0] = 4; βœ” // works fine

// but...
numbers = [5,6,7]; ❌ // won't work
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ the first case will work because we're within the rules , we're not redeclaring the const variable nor we're updating it. but we're mutating it. πŸ˜€


Summary

πŸ‘‰ var: Global/function scoped depending on declaration undefined when accessing a variable before it's declared. can be redeclared and updated.


πŸ‘‰ let: block scoped. can be updated but we can't redeclare.


πŸ‘‰const: block scoped. can not be reassigned nor we can redeclare.

Let me know in comment section if you have any doubt or feedback. it's always worth to give time to the thriving developer community :)

Keep Coding ❀

Hey , Let' ConnectπŸ‘‹

Twitter / Github

πŸ’– πŸ’ͺ πŸ™… 🚩
whoadarshpandya
Adarsh Pandya

Posted on January 22, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Al Shop Fix {Day -27}
100daysofcode Al Shop Fix {Day -27}

September 14, 2021

About Browser {Day - 24}
100daysofcode About Browser {Day - 24}

September 6, 2021

JS Objects {Day -19}
100daysofcode JS Objects {Day -19}

September 1, 2021

Data Types JS {Day -18}
100daysofcode Data Types JS {Day -18}

August 31, 2021