Q:

var Vs let in JavaScript

0

Let's checkout the difference in var and let

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

The main difference is the scope rules:

The let variable can only be available inside the scope it's declared, they are scoped to the immediate enclosing block denoted by { }

The var variable belongs to the global scope or the local scope if they are declared inside a function.

There's also a difference when creating global properties.

Example:

var foo = "I'm added to the global obj as a property.";


console.log(window.foo); / /I'm added to the global obj as a property.


let foo = "I'm not added to the global obj as a property."

console.log(window.foo); // undefined . .

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now