What is the difference between 'var' and 'let' in javascript? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

What is the difference between 'var' and 'let' in javascript?

Both seemed having same functionality :/

5th Jun 2018, 2:35 PM
Shreya Pandey
Shreya Pandey - avatar
5 Answers
+ 5
Let has more limited scope. E.g. you declare a variable within a function with let keyword. if you try to use that variable somewhere outside the function you'll get an error saying the variable hasn't been declared.
5th Jun 2018, 3:14 PM
Haris
Haris - avatar
+ 10
This has a great explanation of the differences. https://stackoverflow.com/questions/762011
5th Jun 2018, 2:47 PM
John Wells
John Wells - avatar
+ 5
https://www.sololearn.com/Discuss/1256607/?ref=app https://www.sololearn.com/Discuss/1248231/?ref=app
5th Jun 2018, 3:02 PM
Louis
Louis - avatar
+ 2
var declares a global variable, let declares a variable within a scope e.g. in a function function sololearn() { let c="sololearn"; window.alert(c); //outputs sololearn } this will work fine but if I use it like: function sololearn() { let c="sololearn"; } alert(c); //outputs undefined because the variable isn't declared with a global scope.
10th Jun 2018, 8:48 AM
Abbhinav Bharadwaj
- 3
let is a constant, while var is not
6th Jun 2018, 4:11 PM
Baribor Saturday
Baribor Saturday - avatar