var vs let & const.

Redeclaration of variable

You can redeclare a variable with var but not with let and const:

var foo = 34;
var foo = 46;

foo; // 46

But if we try to redeclare a variable with const and let, we will get a syntax error:

var foo = …


This content originally appeared on DEV Community and was authored by RabeDatta

Redeclaration of variable

You can redeclare a variable with var but not with let and const:

var foo = 34; 
var foo = 46;

foo; // 46

But if we try to redeclare a variable with const and let, we will get a syntax error:

var foo = 34; 
let  foo =46;

foo;  
// SyntaxError: Identifier 'foo' has already been declared.

Global scope & function scope vs block scope

Var only knows global scope and function scope meaning if you declare a var variable inside an if statement, you can still access it from outside of that if statement, but you can't do with let and const. For example:

var name = "Max"; 

if(name === 'Max'){
  var hobbies = ['Sports', 'Cooking']; 
  console.log(hobbies)  // [ 'Sports', 'Cooking' ]
}

console.log(name, hobbies)  
// 'Max' [ 'Sports', 'Cooking' ]

Notice that you can still access hobbies from outside of that if block. But in the case of let and const, we can't do that since both let & const are block scoped.

var name = "Max"; 

if(name === 'Max'){
  let hobbies = ['Sports', 'Cooking']; 
  console.log(hobbies)  // [ 'Sports', 'Cooking' ]
}


console.log(name, hobbies) 
// ReferenceError: hobbies is not defined

As you can see, we got a ReferenceError. Let and cost only care about curly braces(except for curly braces of an object). You can't access variable defined inside a child block from outside. Also, you can't access variable that you defined inside a nested block.


var name = "Max"; 

if(name){
  {
    let test = 34;
  }

console.log(test)  
// ReferenceError: test is not defined
}

Hoisting: var vs let & cost

  • when it comes to var, JavaScript initializes the hoisted variable as undefined:
console.log(name); //'undefined'

var name = "Rob"
  • when it comes to let & const, the declarations remain uninitialized (Cannot be Hoisted) :
console.log(name);        // ReferenceError: 

Cannot access 'name' before initialization

var name = "Rob"


This content originally appeared on DEV Community and was authored by RabeDatta


Print Share Comment Cite Upload Translate Updates
APA

RabeDatta | Sciencx (2022-01-16T07:05:36+00:00) var vs let & const.. Retrieved from https://www.scien.cx/2022/01/16/var-vs-let-const/

MLA
" » var vs let & const.." RabeDatta | Sciencx - Sunday January 16, 2022, https://www.scien.cx/2022/01/16/var-vs-let-const/
HARVARD
RabeDatta | Sciencx Sunday January 16, 2022 » var vs let & const.., viewed ,<https://www.scien.cx/2022/01/16/var-vs-let-const/>
VANCOUVER
RabeDatta | Sciencx - » var vs let & const.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/01/16/var-vs-let-const/
CHICAGO
" » var vs let & const.." RabeDatta | Sciencx - Accessed . https://www.scien.cx/2022/01/16/var-vs-let-const/
IEEE
" » var vs let & const.." RabeDatta | Sciencx [Online]. Available: https://www.scien.cx/2022/01/16/var-vs-let-const/. [Accessed: ]
rf:citation
» var vs let & const. | RabeDatta | Sciencx | https://www.scien.cx/2022/01/16/var-vs-let-const/ |

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.