Let, Const and Var Variables in Javascript.

Well, there are three different ways to declare variables in javascript. Those are namely let , const and var declarations.

Let us understand the difference between the three of them and use them in our code!


Let Keyword

Let keyword is also known as block scoped operator. It means the variables declared inside curly brackets cannot be altered from the global operator.

For example :

// For example Global Operator 
let aryanGrade = 450; 
// Blocked Scope Operator
{
let aryanGrade = 120; 
console.log(aryanGrade) // Output = 120
}
console.log(aryanGrade) // Output = 450

Const Keyword

The const keyword is very useful as it helps in declaring a variable with an absolute value that cannot be altered within or without scope.

const name = "Aryan"
// Declaring other variable with same name gives an error :
// const name = "Devyansh" 
// Output
//const name = "Devyansh";
  //    ^
// SyntaxError: Identifier 'name' has already been declared

Var Keyword

The var keyword allows you to change the variable data within any scope, whether it's global or blocked scope.

Here's an example of the same:

var rupees = 82;
{
var rupees = 71;
console.log(rupees) // Output = 71
}
console.log(rupees) // Output = 71