This content originally appeared on DEV Community and was authored by Akshay Kurhekar
Let's understand comparison between
var,letandconst.
- Variables defined with
vardeclarations are globally scoped or function scoped andletandconsthas block scope or we can say local scope.
example:
var testVar = 'var'; // global scope
let testLet = 'let'; // local scope
const testConst= 'const'; // local scope
function testScope {
consol.log(window.testVar); // var
consol.log(window.testLet); // undefined
consol.log(window.testConst); // undefined
}
testScope() // output var
- Variables defined with
varcan be Redeclared but withletandconstcannot be Redeclared.
example :
var testVar = 10;
let testLet = 10;
const testLet = 10;
function test{
var testVar = 20; // it will work
let testLet = 20; // it will throw error
const testConst = 20; // it will throw error
console.log(testVar,testLet,testConst);
}
test();
Varcan updated and re-declared.Letcan be updated but not re-declared.Constcannot be updated and re-declared.
example :
var testVar = 10;
let testLet = 10;
const testLet = 10;
function test{
testVar = 20; // it will work
testLet = 20; // it will work
testConst = 20; // it will throw error
console.log(testVar,testLet,testConst);
}
test();
- While
varandletcan be declared without being initialized,constmust be initialized during declaration.
this are all main differences of var, let and const.
Got any question or additions? Please let me know.
Thank you for reading.
This content originally appeared on DEV Community and was authored by Akshay Kurhekar
Print
Share
Comment
Cite
Upload
Translate
Updates
There are no updates yet.
Click the Upload button above to add an update.
APA
MLA
Akshay Kurhekar | Sciencx (2021-08-17T09:07:35+00:00) Var vs Let vs Const in JavaScript. Retrieved from https://www.scien.cx/2021/08/17/var-vs-let-vs-const-in-javascript/
" » Var vs Let vs Const in JavaScript." Akshay Kurhekar | Sciencx - Tuesday August 17, 2021, https://www.scien.cx/2021/08/17/var-vs-let-vs-const-in-javascript/
HARVARDAkshay Kurhekar | Sciencx Tuesday August 17, 2021 » Var vs Let vs Const in JavaScript., viewed ,<https://www.scien.cx/2021/08/17/var-vs-let-vs-const-in-javascript/>
VANCOUVERAkshay Kurhekar | Sciencx - » Var vs Let vs Const in JavaScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/17/var-vs-let-vs-const-in-javascript/
CHICAGO" » Var vs Let vs Const in JavaScript." Akshay Kurhekar | Sciencx - Accessed . https://www.scien.cx/2021/08/17/var-vs-let-vs-const-in-javascript/
IEEE" » Var vs Let vs Const in JavaScript." Akshay Kurhekar | Sciencx [Online]. Available: https://www.scien.cx/2021/08/17/var-vs-let-vs-const-in-javascript/. [Accessed: ]
rf:citation » Var vs Let vs Const in JavaScript | Akshay Kurhekar | Sciencx | https://www.scien.cx/2021/08/17/var-vs-let-vs-const-in-javascript/ |
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.

