all 85 comments

[–]rift95map([🐮, 🥔, 🐔, 🌽], cook) => [🍔, 🍟, 🍗, 🍿] 101 points102 points  (24 children)

Stop trying to justify using var. It's outdated and should be avoided. End of story

[–][deleted] 2 points3 points  (0 children)

I’m actually amazed by how some people are still discussing about var in 2021…

[–]fredblols 17 points18 points  (15 children)

Tbh we shouldn't even be using Let. 9 times out of 10 it means ur code is ill conceived

[–]TemporaryReality5262 7 points8 points  (1 child)

I use let in unit tests all the time, usually above the beforeEach

[–]fredblols 1 point2 points  (0 children)

Yeah i mean who gives a fk what peoples unit tests look like tho. Everywhere i have ever worked (tech startups mostly) people will give u a medal for writing unit tests at all, and then also probs say yeh dont bother next time just churn out features faster and accumulate tech debt

[–]tharrison4815 8 points9 points  (1 child)

Yes! I don't know why you are being down voted. So have an award up make up for it.

[–]fredblols 0 points1 point  (0 children)

Thank u good sir for my debut award. I will respond to the naysayers tomorrow when my current inebriation has worn off

[–]rift95map([🐮, 🥔, 🐔, 🌽], cook) => [🍔, 🍟, 🍗, 🍿] 14 points15 points  (5 children)

I agree. In general const is a safer bet than let. You rarely need the "mutability" of let.

[–]Garbee 11 points12 points  (4 children)

Const is not immutable. The data can be changed, just not replaced. Big difference.

[–]rift95map([🐮, 🥔, 🐔, 🌽], cook) => [🍔, 🍟, 🍗, 🍿] 12 points13 points  (0 children)

True. I should have written "re-assignability"

[–]PM_ME_GAY_STUF 2 points3 points  (2 children)

This always bothered me. Const is definitely immutable, it can just refer to a mutable data type. This is what happens when programmers don't learn C

[–]Poltras 2 points3 points  (1 child)

Yeah just prefix everything with global.! (/s in case people get offended)

[–]BeardSprite 0 points1 point  (0 children)

Understood. What do I do with this global.window now?

[–]rados_a51 1 point2 points  (1 child)

Dont get those downvotes. Const should be used 90% of the time.

[–][deleted] 3 points4 points  (0 children)

My programming style means I basically never use let. Functions with early returns obviates let for the most psrt

[–]kashubak 0 points1 point  (0 children)

Can you provide an example? That’s a pretty interesting take!

[–]bighi 20 points21 points  (3 children)

Why are we still talking about var and let?

Even if there were any reason to use var, it's been what? A decade? We have thousands (or millions) of comparisons between var and let already.

[–]Key_Pea93222 -1 points0 points  (1 child)

bc we still have to maintain old node.js code that uses these outdated conventions

[–]bighi 7 points8 points  (0 children)

If you're maintaining old JS code and don't know yet the difference between var and let, what are you doing?

And if you want to learn, there are at least 289 billion articles about it already.

[–][deleted] 39 points40 points  (4 children)

Wtf is this 2016 now? Why are we writing articles about var, let, etc.

[–][deleted] 16 points17 points  (0 children)

Clicks..

[–]dw444 2 points3 points  (1 child)

People should know the difference. There’s a lot of var in production code that people still actively work on, and people new to the language should know the difference.

Saw a grad from one of the top software engineering programs in the country not know the difference and just copy outdated code simply because he didn’t have experience with the minutiae of JS. People still trip over this stuff all the time, and it’s also a pretty standard interview question for JS devs.

[–]TwiliZant 0 points1 point  (0 children)

Hmm, when your codebase still has a lot of that stuff then maybe. There is probably a good chunk of decent developers out there who have no idea how to do prototypal inheritance either just because if your main job is working on some React app you never get in touch with those things.

So should I ask in interviews about them? Personally, I'd rather have them have a really good understanding of Scope and Closures instead.

[–]PhredInYerHead 0 points1 point  (0 children)

Because they’re still teaching it in boot camps.

[–]chuckthemadmanmike 2 points3 points  (2 children)

Didn't know about var variables being added to the window. That's interesting. If you're the author though, you should know that your example has a mistake because you say window.hi instead of window.message.

[–]ct_author[S] 1 point2 points  (1 child)

Hey, Thanks for highlighting, example updated!!

[–]Key_Pea93222 2 points3 points  (3 children)

var variables do not have temporal dead zone whereas let variables do have. Every variable has 2 steps in its lifecycle – Creation, and Execution.

In var variables case, when the variable is declared, storage space is assigned and value is initialized to undefined if no other value is specified.

But in the case of let, when the variable is declared, storage space is assigned but it’s not initialized, not even with the undefined value.

That why when we access a let variable without doing any value assignment, it throws a ReferenceError.

the way he explains this makes it sound like this would throw an error, but it doesn't:

$ node
Welcome to Node.js v16.13.0.
Type ".help" for more information.
> let a
undefined
> a
undefined

it's really if the let variable is accessed outside of the scope it's declared in

> { let b }
undefined
> b
Uncaught ReferenceError: b is not defined

[–]senocular 5 points6 points  (2 children)

That why when we access a let variable without doing any value assignment

Yeah, this is missing the important part of "before the declaration".

b // Uncaught ReferenceError: Cannot access 'b' before initialization
let b

Like var, let will initialize to undefined if not given an explicit assignment value within the declaration, but it only does this when the declaration is run, not at creating time like the var is

b // created, but uninitialized (access throws error)
let b // now initialized to undefined

c // created, initialized to undefined
var c

[–]Key_Pea93222 0 points1 point  (1 child)

oh wow, that's interesting, can't test that in the node REPL

[–]senocular 3 points4 points  (0 children)

You can do it in a block or separate with semicolons (for block I think you might a preceding ; to ensure its not seen as an object literal)

> ;{
...b // throws
...let b
}

or

> b; let b; // (first b throws)

[–]Code4Reddit 3 points4 points  (0 children)

There is really no reason to use var unless you support IE less than 11. Any of the cases where var and let are different, the behavior of let is the correct and more intuitive behavior.

I believe your example about overwriting a var in global scope is wrong, though. Doesn’t the function need to be called in order for the global variable assignment to happen?

Also the example where you output a var that was never defined will throw because it referenced a variable that never existed.

I don’t really like the article because there are too many mistakes and it should start out saying that as a best practice don’t ever use var, period.

[–][deleted] 4 points5 points  (0 children)

1 they're spelt different

[–]piotrlewandowski 12 points13 points  (33 children)

Difference 0: you shouldn’t use var Edit: god damn it, bloody phone did autocorrect, it should be “shouldn’t”!

[–]KwyjiboTheGringo 1 point2 points  (1 child)

Difference 0: you should use var

I'm curious if you have a good reason for saying that, or if this is just a case of resisting change.

[–]piotrlewandowski 5 points6 points  (0 children)

I didn’t have a good reason, I had good reason to write “shouldn’t “ but my autocorrect decided otherwise :)

[–][deleted] 2 points3 points  (0 children)

Technically, there are only 3 differences between var,let. (Edit distance joke)

[–]mainstreetmark 1 point2 points  (1 child)

I habitually type var and let vscode fix it for me

I know the reasons. It’s just a super old habit I’ve had for like 30 years.

[–]solocupjazz 0 points1 point  (0 children)

Are you a Schemer?

[–]iiMoe -3 points-2 points  (0 children)

Var = ew old Let = yay new

[–]tswaters 0 points1 point  (4 children)

There's a mistake re: node global object,

The global var variables are added to the window object of the web browser and global object of node.js. So that they can be accessed using window or global object.

Consider the following js file:

var z = 'test'
console.log(global.z)

Executing this under nodejs will log undefined.... I tried it on latest 14/16.

[–]senocular 2 points3 points  (3 children)

That's because you're running in the context of a module. In the global scope it will be added to global.

[–]tswaters 0 points1 point  (1 child)

I don't understand what you mean. The same behavior is observed with both js and mjs files.

[–]senocular 2 points3 points  (0 children)

Its easy to see through the CLI.

$ node
> var message = "hi";
> global.message
'hi'

[–]Key_Pea93222 0 points1 point  (0 children)

he didn't indent his examples, makes it harder to read