Let vs var vs const

Can any one help me understand how these three different ways to create a variable really works within a function? From what i already learn var is functions scoped let and const is block scope. Whats confusing to me is does this mean that if I create a variable using var I can use that variable anywhere inside the function and let/const can only be used within a specific block for example a if/switch statement? Also how do you even know when its the right time to use a specific type of variable? Thanks in advance
19 Replies
Joao
Joao•9mo ago
In short, any variable created with let or const is only accessible within a scope, which is essentially any code within curly brackets. This includes if statements, for loops, functions, etc. This means that you can have two variables with the same name, as long as they are not within the same scope. Not that you should intentionally do this, on the contrary, but it is technically possible. As for when you choose which one to use, it's simple: use let when you expect your variable to be reassigned; otherwise, use const. Don't use var. If you ever have the feeling that using var may solve a particular problem that you are having, it's a clear sign that you need to reconsider the entire approach you are taking.
dellannie
dellannie•9mo ago
Okay that was going to be my next question. From what i'm seeing and reading everything kind of advise against using var and I still cannot figure out why? thats just my curiosity kicking in trying to understand why
Joao
Joao•9mo ago
One note regarding let and const. If you declare an array or an object, you still can modify the value of the variable. For example, you can push items into an array, or add new properties to an object. That is not the same as reassigning the variable itself. Therefore you can declare arrays with const because this only means you won't be reassigning the variable into something else, not that the array won't be modified somehow.
13eck
13eck•9mo ago
There are, like, five different questions here :p But I'll try to answer them as best as I can. var is either function scoped or globally scoped. So any var declared in a function is scoped to that function and won't "leak" out. But any other place you declare a var it will "leak" into the global scope: for loops, if statements, switch statements, etc. function or nothing. It is for this reason that var is frowned upon. let and const are pretty much the same, with the only difference being that you can re-assign a let but can't re-assign const:
// perfectly fine
let a = "hello";
a = 3.14159;

// throws an error
const b = "world";
b = 38;
// perfectly fine
let a = "hello";
a = 3.14159;

// throws an error
const b = "world";
b = 38;
Both let and const are block scoped, so any place you have curly braces {} the variables won't "leak" out. So a for loop, a while loop, an if statement, etc. that's why let and const are preferred.
13eck
13eck•9mo ago
Here's a very basic example of the differences:
No description
dellannie
dellannie•9mo ago
I just learn this like 20 mins ago lol
13eck
13eck•9mo ago
My personal opinion is that you should default to const, to prevent yourself from accidentally overwriting a variable. Change it to let if you find yourself wanting to re-declare it. For example, if you're looping through something and it's updating a variable
Joao
Joao•9mo ago
One of the problems const and let were supposed to solve was the risk of name collisions. You don't want to accidentally overwrite a variable from a library that you imported from npm or something.
13eck
13eck•9mo ago
And yes, Joao is right: const only prevents you from re-assigning/re-declaring a variable. But "complex" (IE non primitives) can be modified. Primitives are: * Null * Undefined * booleans * numbers * bigints * strings * symbols Oh, yeah, that's a good point! The following is valid JS:
var a = 77;
// some more code
var a = {name: "13eck"}
var a = 77;
// some more code
var a = {name: "13eck"}
But this will throw an error:
let a = 77;
// some more code
let a = {name: "13eck"}
let a = 77;
// some more code
let a = {name: "13eck"}
dellannie
dellannie•9mo ago
I've been noticing this tatic a lot. Even on som tutorials I watch and read const is the main but then gets changed to let if the variable is getting changed
13eck
13eck•9mo ago
Defaulting to let or const is really a personal preference. I've seen valid arguments either way. Regardless of which you use, if you try to let a = something or const a = something more than once you'll get an error.
Joao
Joao•9mo ago
That's another advantage. If you default to use const everywhere and I'm reading your code and I suddenly see a variable declared with let, I immediately I'm aware that somewhere this will change values. So this syntax also signifies intent.
dellannie
dellannie•9mo ago
This helps me out a lot. Thanks for the input guys I appreciate this
Joao
Joao•9mo ago
@dellannie Btw, in this code example from just above https://discord.com/channels/436251713830125568/1150169594036764682/1150174315002740817 Note that you can change a variable from one type to another e.g., from a number to an object, or string, or whatever. Try not to do that. It's fine to update the value from 21 to 42, but not from 42 to "John". It's possible but... meh, not so good in general.
dellannie
dellannie•9mo ago
okay
Joao
Joao•9mo ago
The only exception tothat would be if you need to somehow "destroy" the variable, so that would be assigning it to null. That's actually okay and quite useful actually, but yeah try to stick to one type per variable.
dellannie
dellannie•9mo ago
okay. Havent got that far yet but all this stuff now I have a better understadning of how these variables work
Joao
Joao•9mo ago
Great, the better your understanding of the fundamentals the easier it'll be to learn everything else 🙂
dellannie
dellannie•9mo ago
Thats all I've been working on for the last few weeks trying to just understand how the fundamentals work.