Var, Let and Const: Description and Differences

Introduction

In JavaScript, var and let are two new keywords that let you declare variables and constrain them to a given scope.

we use the var keyword to declare a variable.

A variable can hold any value, including numbers, strings, and booleans. We use the let keyword to declare a block of code that can be used in multiple places later on in our program. var is used to declare a variable that is local to just one line of code. This means that only one line of code will have access to this variable:

var name = 'Aniket';

The let keyword is used when declaring a block of code that should be reusable throughout our program. For example:

let myName = 'Aniket';

In JavaScript, var and let are two very similar constructs. They both declare variables that are local to the current scope and can't be accessed from outside of that scope. They are also both used to create new variables inside a function.

The const declaration creates block-scoped constants, much like variables declared using let keyword. The value of constant can't be changed through reassignment (i.e by using the assignment operator), and it can't be redeclared (i.e through a variable declaration)

const myName = 'Aniket';

Differences

You might be wondering, "What's the difference between var and let?"

Scope

Global or Function based


Start to end of the function inside of the function

Block



Start to end of the current scope anywhere.

Block



Start to end of the current scope anywhere.

Re-declaration

We can re-declare it in the same scope.

We can’t re-declare it in the same scope.

We can’t re-declare it or re-initialize it in the same scope.

Update value

Values can be updated

Values can be updated

Values cannot be updated

Hoisting

Hoisting at the top of Global scope.

Hoisted at the top of some private scope and only available after assigning value.

Hoisted at the top of some private scope and only available after assigning value.

Well, var is a variable that's defined using the var keyword. It can be used to declare local variables that are declared inside a function or in a block comment.

let is a block-level declaration in JavaScript. It can't be used inside a loop or an if clause, but it can be used to declare local variables that won't be visible outside of their scope.

VAR and LET are two different types of variables. VAR is a variable declared with the var keyword, while LET is a variable declared with the let keyword.

VAR is more flexible than LET because it allows you to declare multiple variables in one statement without having to declare each variable individually. For example, let x = 2; var y = 3; var z = 4; will assign 3 to x, 4 to y, and 5 to z. However, var x = 2; let y = 3; let z = 4; will only assign 1 to x and 2 to y because they are not declared in the same block as their values (the scope of their scope). LET gives you additional control over which variables are initialized within a block by providing a scope option where you can choose whether or not to initialize objects that have been created outside of the block.

var keyword in JavaScript

var is a JavaScript keyword that you use to declare a variable. It's used to define a local value for the scope of the current function, as well as its scope outside of that function.

Scope : Global or Function scope, means variable defined outside the function can be accessed globally, and variables defined inside a particular function can be accessed within the function.

EXAMPLE 1

var a = 50
        function bi(){
            console.log(a)
        }
    bi();
    console.log(a); 

Output

50
50

EXAMPLE 2

//Program to greet the user

//Declaring a function


function welcome(name){
    console.log("Welcome to " + name + ":)");
}

//Assigning a Variable
let name = ("Board Infinity");

//Calling a Function
welcome(name);

OUTPUT

Welcome to Board Infinity

write your code here: Coding Playground

let keyword in JavaScript

The let keyword is an updated or improved version of the var keyword.

Scope : it can’t be accessible outside the particular block.

EXAMPLE

let a = 50;
    function bi() {
        let b = 40
        console.log(b);
        console.log(a);
    }
    bi();

OUTPUT

40
50

write your code here: Coding Playground

const keyword in JavaScript:

Const is a keyword that you can use to define a constant value in JavaScript, Constants are similar to variables except that the values cannot be reassigned.

EXAMPLE

const x = 10;
console.log(x)

OUTPUT

10

write your code here: Coding Playground

Conclusion

let is a block declaration statement that declares an object-like block of code. This can be used to declare variables that live outside of a function and have access to the global scope and  var is a shorthand that you can use to declare the same variable more than once.