April 01, 2020

Part 1: Node JS Tutorial-var vs let vs const in JavaScript

While declaring any variable we can use either var, let or const. In JavaScript, variables are initialized with the value of undefined when they are created. What that means is if we try to log the declaration variable, we’ll get undefined. e.g:

var name;
console.log(name); // this will print 'undefined'

Scope defines where variables and functions are accessible inside of your program. In JavaScript, there are two kinds of scope - global scope, and function scope.

If we create a variable with var, that variable is 'scoped' to the function it was created in and is only accessible inside of that function or, any nested functions.

function sum(a, b){
    var c= a+b;
return c;
}

var total=sum(7, 2);
console.log(total); //it will print 9
console.log(c); //Reference Error

Since we are trying to access variable c, which was declared inside sum function outside the function scope we will get reference error. variable c can be used only side the sum itself or any nested functions inside of sum.

function sum(a, b){
    var c= a+b;
function printSum(){
console.log(c); // it will print 9
}
return c;
}

Are the variables declared inside of it accessible outside of it? To understand this, let's take an example:

function sum(a, b){
    console.log("inputs:"+a+", "+b)
    function getSum(){
        var c=a+b;
    }
    console.log("Sum is:"+c); // it will print the value of c
}

In the above example, we have declared a variable c inside a function getSum(), which is the nested function (its inside sum)

var VS let
The main difference between var and let is that instead of being function scoped, let is block scoped.

function sum(a, b){
    console.log("inputs:"+a+", "+b)
    function getSum(){
        let c=a+b;
    }
    console.log("Sum is:"+c); // ReferenceError: c is not defined
}

In the above example, we have declared a variable c inside a function getSum(), which is the nested function (its inside sum). Now if we try to access the variable c outside getSum() we will get an error message 'ReferenceError: c is not defined', which tells us is that variables declared with let are block scoped, not function scoped.

In short var is function scoped and let is block scoped. Also, if you try to access a variable declared with let before it’s declared, instead of getting undefined (like with those variables declared with var), you’ll get a ReferenceError.

let VS const
const is almost exactly the same as let. However, the only difference is that once you’ve assigned a value to a variable using const, you can’t reassign it to a new value. e.g:

let firstName = 'Himaanshu'
const lastName = 'Shuklaa'

firstName = 'Ratan' // it will work
lastName = 'Tata' // TypeError: Assignment to constant variable.

Let's take another example:

const person = {
  name: 'Himaanshu Shuklaa'
}

person.name = 'Ratan Tata' // it will work
person = {} // Assignment to constant variable.

In the above example we have changed the property (name) of an object and changing a property on an object isn’t reassigning it, so even though an object is declared with const, that doesn’t mean you can’t mutate any of its properties. It only means you can’t reassign it to a new value.

-K Himaanshu Shuklaa

No comments:

Post a Comment