Difference between var and let
Go to solution
Solved by 0x21,
Variables declared by let have their scope in the block for which they're defined, and any contained blocks. The scope of var is that of the entire enclosing function. Also, unlike var which will initialise with the value undefined, let variables are not initialised until evaluated.
function varExample() { var n = 1; if (n === 1) { var n = 2; console.log(n); // 2 } console.log(n); // 2 } function letExample() { let n = 1; if (n === 1) { let n = 2; console.log(n); // 2 } console.log(n); // 1 }
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now