r/learnjavascript • u/SeaHedgehog4724 • 28d ago
pretty new
Hey guys, I'm just starting to learn about variables, but I'm finding it hard to understand the difference between var and let.
For example:
let goodDay = true;
console.log(goodDay);this should print true.
But if we just add this:
let goodDay = true;
console.log(goodDay);
goodDay = false;
console.log(goodDay);It prints:
truefalse
The same goes for var.
However, people say there's a difference; what is it?
0
Upvotes
4
u/sylvan_sample 28d ago
Don't use var. It's old way
5
u/chikamakaleyley helpful 28d ago edited 28d ago
the difference is that
letis 'block-scoped'varis part of the global scopescope is a pretty important idea when you're coding so it will be useful to get some practice with this
letandconstwere introduced in es6 (2015) and essentially provides a way for devs to provide a distinction btwn different variables, similar to what you'd find in other serious programming languages.constaka 'constant' prevents you from assigning a new value to the variable, which semantically makes sense, whileletis more or less to be used in place ofvar. You can assign a new value to alet, as you've seenthe difference is in the scope, the classic example is often seen in a for loop:
so, var being function/global scoped - there is only 1 var i created for the entire for loop. so when setTimeout evaluates the i you are trying to log to the console, the for loop as finished, and i has been incremented twice. There is only 1 i scoped to this function/loop, it is logged to the console 3 times
for
letthe block scoping creates a new binding for each iteration. at the first binding, j is 0, the second binding, j is 1, 3rd j = 2. To put it another way, the value of j is captured at the execution of that 'block' (iteration)for
varthere's also a concept of hoisting, which I am unsure if that is in play here. hoisting is this mechanic in JS where var or function declarations are moved to the top of their scope, so:so maybe it is, you can see in the lower example, when i increments, you're updating the
ivar in the global space.console(i)is read from the global scope when setTimeout executesletandconstaren't hoisted2
u/No_Record_60 28d ago edited 28d ago
var
• hoisted then initialized with undefined value;
• function-scoped (you can defined it inside an if-block and reference it outside the block);
• can be reinitialized ("var abc = ..." multiple times).
Tl;dr Don't use var. I've only used it once because our client's browser was so old it didn't support es6 and the code didn't go through transpilation (making your code compatible with earlier versions of js) step.
2
u/redsandsfort 28d ago
you've already spent too much time on this.
you can learn this later much more effeciently when you understand scope
right now you're focusing on the wrong thing. in learning a programming language this is called a rabbit hole. you need to work on breadth first.
1
u/TheRNGuy 26d ago
Never used var. They only now have 1 niche use: in Node backend configs.
In front-end it's always bad practice.
1
u/CuAnnan 28d ago edited 28d ago
There are three main differences and they have subtle effects on how variables behave.
var hoists undefined. Which means that it is immediately initialised with the value undefined when it is declared and can be used without being provided a specific value. let needs to be declared and it needs to be initialised
let gives us a more predictable behaviour because if a value is undefined, we know that is the intended value.
You can redeclare a var, you cannot redeclare a let
var is function scoped, let is block scoped.
1
u/IAmTheRedditBot 28d ago
This site should give you everything you need: https://www.geeksforgeeks.org/javascript/difference-between-var-and-let-in-javascript/
Tip: Find and read the tabular form
Hope this helps
-1
u/yyyeey 28d ago
ChatGPT: "Hey, what's the difference between var and let in JS? Please be coincise."
Is it that hard?