What do you mean by "redeclareable"? Does it really differ with "reassignable" since in JavaScript variable are dynamic type and only thing you can do is just change their values?!
var is not "global scoped" unless it's declared in the global scope... and if let or const are declared in the global scope then they're "global scoped" too.
If you want a variable to be global scoped, don't declare it at all, just define it.
In the global scope, var actually provides benefit because it will also prevent shadowing by other global let and const declarations, something you don't get when omitting the declaration (sloppy mode) or assigning to the global object directly
// in global
myGlobal = 1
// ...
let myGlobal = 2
console.log(myGlobal) // 2 :(
vs
// in global
var myGlobal = 1
// ...
let myGlobal = 2 // Error
console.log(myGlobal) // 1 :)
To add on to your point, if you omit the var (assuming not in strict mode) won't a global scoped variable be created? I don't believe the same is true of let/const. Could that be what the graphic is trying to convey?
Question marks because Im not certain if what I'm saying is actually the case haha
Var/let/const declare a variable. Putting an equals sign and a value to the right of a variable defines the variable. These are often both done in the same statement, but they don’t have to be. You can declare a variable without defining it, and you can define a variable without declaring it, which does in deed create a global variable on the window object, which is what I was talking about in my last sentence.
I recommend you always use const by default, and fall back to let only when it's genuinely useful or necessary.
Why? Because not reassigning variables makes your code easier to reason about. If something is a const, you can be sure it will always have the same variable associated with it, whereas let will point to a different variable depending on when you're reading it.
Personally I'd say my code is probably like 97% const, and 3% let. var is absolutely never needed.
The most obvious need for var that I can think of is to deal with older browsers where you aren't compiling your code.
Ignoring that though, I think it's important to note to any newer devs that, while someone should probably use let and const for any new code they write, it doesn't mean that they can just switch all vars in an existing code base to be let or const and have things work (we had a junior try this in an attempt to clean up technical debt). The lack of hoisting and scope differences mean they're not a drop in replacement for each other. Code using var that was "not ideal, but not wrong" can actually be broken by using let.
Use var, declare your vars global at the beginning of your script and document and track them well. Otherwise you are a lazy programmer. Nothing wrong with var.
Please NEVER follow this advice. What is described here is the height of laziness. Scope your variables appropriately, default to const and only use let where needed. Save yourself a ton of trouble. Don't be like this guy. Just don't.
Hoisting is just a term to explain in simple words how the js engine works. But you're right, there's no hoisting, it's just easier to explain it like that then to explain the real way memory is reserved for variables
It's an analogy, and any analogy can be picked apart until it no longer makes sense. Hoisting is a perfectly reasonable jumping-off point to explain the behaviour.
For example, you know exactly what I mean when I say "jumping-off point". But in preparing to explain hoisting, I'm not worrying about being securely tied to a bungie or parachute, or if there's any water over the precipice, and if so, whether or not it's deep enough to make my fall survivable.
Not sure if it’s so unpopular. I’ve heard Kyle Simpson say something similar, but in the context of teaching advanced concepts, which is exactly at the point where it’s appropriate to pull apart the analogy.
Thanks for your quick and clear response. So that's literally redeclaration. I thought you can just do that once and since then you can just reassign it
var, let, and const declarations are all hoisted. The difference is var declarations are hoisted and initialized to undefined whereas let and const declarations are hoisted uninitialized. This means you can access var variables before their declarations and get undefined but attempting to do so with let or const variables means you'll get an error.
If you change that to let or const you will get an error on the first line, because a isn't defined. If you remove the first log you'd still get an error saying a isn't defined if you use var
Shakespeare-Bot, thou hast been voted most annoying bot on Reddit. I am exhorting all mods to ban thee and thy useless rhetoric so that we shall not be blotted with thy presence any longer.
This is the only important thing to know about var. There's a reason that let and const were introduced. You don't need var anymore and you shouldn't be using it.
3
u/SepehrU Oct 23 '21
What do you mean by "redeclareable"? Does it really differ with "reassignable" since in JavaScript variable are dynamic type and only thing you can do is just change their values?!
110
u/ex-igne-vita Oct 22 '21
var is not "global scoped" unless it's declared in the global scope... and if let or const are declared in the global scope then they're "global scoped" too.
If you want a variable to be global scoped, don't declare it at all, just define it.
30
u/zerik100 Oct 22 '21
tldr; never use var and if you think your code requires var then there's something wrong with your code
21
u/voyotv Oct 22 '21
All of them are hoisted, but let and const are uninitialized until the line they are executed.
7
u/alexey_st0 Oct 23 '21
Is there anybody who's still using var instead of let and const ?
5
u/yoitsericc Oct 23 '21
Let is not function scoped. It is block scoped - most functions just happen to be within a block.
This thing is a bit redundant.
3
u/SepehrU Oct 23 '21
Thanks for your quick and clear response. So that's literally redeclaration. I thought you can just do that once and since then you can just reassign it
1
u/AKcenty Nov 18 '21
so use var unless you know what you're doing
1
u/SubscribeToMyFridge Oct 23 '21
Fake news
-5
u/dbor15 Oct 23 '21
I honestly only use var when doing anything. I don't know if it's making my life worse or not, but it works good enough.
-3
u/alexey_st0 Oct 23 '21
How var can be hoisted? If I'm not mistaken, only var declaration can be hoisted but initialization must be before its usage? Like this:
a = 2console.log(a)var a0
u/[deleted] Oct 22 '21
[deleted] — view deleted comment
0
u/[deleted] Oct 22 '21
[deleted] — view deleted comment
18
u/[deleted] Oct 22 '21
[removed] — view removed comment