Var vs Let vs Const in JavaScript

Let’s understand comparison between var, let and const.

Variables defined with var declarations are globally scoped or function scoped and let and const has block scope or we can say local scope.

example:

var testVar = ‘var’; // …


This content originally appeared on DEV Community and was authored by Akshay Kurhekar

Let's understand comparison between var, let and const.

javascript

  • Variables defined with var declarations are globally scoped or function scoped and let and const has 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 var can be Redeclared but with let and const cannot 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(); 
  • Var can updated and re-declared.
  • Let can be updated but not re-declared.
  • Const cannot 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 var and let can be declared without being initialized, const must be initialized during declaration.

diffrence between var vs let vs const

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
APA

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/

MLA
" » 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/
HARVARD
Akshay 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/>
VANCOUVER
Akshay 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.

You must be logged in to translate posts. Please log in or register.