DEV Community

Cover image for Difference between let and var
Passionate Coder
Passionate Coder

Posted on

Difference between let and var

So I am here with a problem statement which is being asked in interview many times.

let a = 'some value'
var b = 'some other value'

what will the output of

window.a and window.b

So the answer is

window.a will be undefined
window.b = 'some other value'

Reason :

var creates properties of the window object when declared globally

but let (same with const) does not create properties of the window object when declared globally

Difference between var and let

https://www.youtube.com/watch?v=e0QcsWWFXkc

Hope this helped somebody and thank you for reading!

Top comments (2)

Collapse
 
pavelee profile image
Paweł Ciosek

thanks!

Collapse
 
lyrod profile image
Lyrod

This is not always on windows object, this add the var in the current context, right?

So if I make a function in a function, thid will add it in the function context?