InterviewHubPRO
Section Overview
Q22

Question 22 of 521

Part 1: Core Objects, Prototypes, Functions & Scope (1 - 53)

4%
JavaScript Internals (521 Qs)•Part 1: Core Objects, Prototypes, Functions & Scope (1 - 53)

What is the difference between let and var

You can list out the differences in a tabular format

varlet
It has been available from the beginning of JavaScriptIntroduced as part of ES6
It has function scopeIt has block scope
Variable declaration will be hoisted, initialized as undefinedHoisted but not initialized
It is possible to re-declare the variable in the same scopeIt is not possible to re-declare the variable

Let's take an example to see the difference,

javascript
function userDetails(username) {
  if (username) {
    console.log(salary); // undefined due to hoisting
    console.log(age); // ReferenceError: Cannot access 'age' before initialization
    let age = 30;
    var salary = 10000;
  }
  console.log(salary); //10000 (accessible due to function scope)
  console.log(age); //error: age is not defined(due to block scope)
}
userDetails("John");