Always declare JavaScript variables with var,let, or const. Var , let, const all three are use to create variable in javascript but there are following differences.
Var
The var keyword is used in all JavaScript code from 1995 to 2015.
The let and const keywords were added to JavaScript in 2015.
If you want your code to run in older browsers, you must use var.because a is defined using varif(10>5) { var a = 2; } console.log(a); // 2 it will work
Let
The let keyword was introduced in ES6 (2015).
Variables defined with let cannot be Redeclared.
Variables defined with let must be Declared before use.
Variables defined with let have Block Scope.a can be access only within {} because it is declared using letif(10>5) { let a = 2; } console.log(a); // error
Const
The const keyword was introduced in ES6 (2015).
Variables defined with const cannot be Redeclared.
Variables defined with const cannot be Reassigned.
Variables defined with const have Block Scope.because a is defined as const it scope is only within {}if(10>5) { const a = 2; a = 100 // error because it is const } console.log(a); // error
Must be Assigned
JavaScript const
variables must be assigned a value when they are declared:
const PI = 3.14159265359;