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';
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';
You might be wondering, "What's the difference between var and let?"
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.
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.
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.
The let keyword is an updated or improved version of the var keyword.
Scope : it can’t be accessible outside the particular block.
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.