Understanding var, let and const in Javascript

Variables play an essential half in any Programming Language as they act because the containers to retailer the Information
Variables are just about required to carry out any operation

Javascript offers 3 ways of declaring the variables particularly:
1) var
2) let
3) const

let’s discover every variable kind and its use circumstances.



Var:

var declarations had been predominant earlier than the appearance of ES6.

var greetings="Whats up Javascript"
Enter fullscreen mode

Exit fullscreen mode

It is among the best methods to declare the variables.
var variables may be re-declared and re-initialized.

Nevertheless, there are points with the variables declared utilizing var and which led to new methods of variable declarations



Scope of var:

Scope primarily means the place the variable is offered to be accessed. Variables declared as var are world scoped or perform/native scoped.

var globalVar="Welcome to my weblog";

perform readBlog() {
  var localVar="You might be studying Loopy Js";
}

console.log(globalVar); // 'Welcome to my weblog'
console.log(localVar); // Reference Error: localVar shouldn't be outlined
Enter fullscreen mode

Exit fullscreen mode

Right here, the variable globalVar may be accessed anyplace throughout this system because it doesn’t reside inside any perform and is a globally scoped variable.

whereas the localVar declared contained in the perform can’t be accessed outdoors.



Points with var

  • Variables may be re-declared and re-initialized with var:

When a variable is said utilizing var it may be re-declared and its worth may be modified.

var greetings="Whats up World!";
var greetings="Whats up Javascript";
Enter fullscreen mode

Exit fullscreen mode

because the variable with the identical identify is redeclared in the identical scope it confuses.

Variables declared as var may be accessed earlier than they’re declared that is attainable due to a particular characteristic referred to as Hoisting.

console.log(greetings); // undefined
var greetings="Whats up World!";
Enter fullscreen mode

Exit fullscreen mode

  • var exists past the block during which it’s declared:

Variables declared as var exist past the block during which they’re outlined if not outlined inside a perform.

if (true) {
var a="Whats up"
}

console.log(a);
Enter fullscreen mode

Exit fullscreen mode



let:

let is now most well-liked variable declaration and comes as an enchancment to var declarations.

let greetings="Whats up Javascript"
Enter fullscreen mode

Exit fullscreen mode



Scope of let:

Variable declared as let is block-scoped.

A block is a piece of code that resides in {}.
let variables outlined inside a block can’t be accessed outdoors it.

perform greetings() {
    let greetings="Whats up Javascript"
}

console.log(greetings); // Reference error
Enter fullscreen mode

Exit fullscreen mode

Enhancements over var:

  • let variables may be up to date however not re-declared
let greeting = 'hey'
greeting = 'Hello, As an alternative'

console.log(greeting); // 'Hello, As an alternative'
Enter fullscreen mode

Exit fullscreen mode

this may lead to an error

let greeting = 'Whats up';
let greeting = 'Hey'; // this may lead to an error
Enter fullscreen mode

Exit fullscreen mode

Nevertheless, if the identical variable is outlined in numerous scopes, there might be no error.

  • Hoisting of let
    let variables are additionally hoisted however not like var, let shouldn’t be initialized as undefined and can’t be accessed till some worth is assigned to let variables.
console.log(greeting); // ends in a reference error.
let greeting = 'Whats up';
Enter fullscreen mode

Exit fullscreen mode



const:

const variables are used when we have to assign a continuing worth to a variable. const variables have to be assigned with a worth as quickly as they’re declared.

const greeting = 'Whats up';
console.log(greeting); //Whats up
Enter fullscreen mode

Exit fullscreen mode



Scope of const:

const variables are block-scoped much like let.

perform greetings() {
    const greetings="Whats up Javascript"
}

console.log(greetings); // Reference error
Enter fullscreen mode

Exit fullscreen mode

Enhancements over var:

  • const variables can’t be up to date or re-declared
const greeting = 'hey'
greeting = 'Hello, As an alternative' // syntax error
Enter fullscreen mode

Exit fullscreen mode

  • Hoisting of const
    const variables are additionally hoisted however not like var, const shouldn’t be initialized as undefined and can’t be accessed till some worth is assigned to const variables.
console.log(greeting); // ends in a reference error.
const greeting = 'Whats up';
Enter fullscreen mode

Exit fullscreen mode

Bought any questions or recommendations? let me know
Thanks for studying 🙂

Add a Comment

Your email address will not be published. Required fields are marked *