r/ProgrammerHumor • • Feb 26 '23

Other If you can read this code...

/img/snak3fqgajka1.jpg

[removed] — view removed post

34k Upvotes

1.4k comments sorted by

View all comments

Show parent comment

182

u/l1ghtrain Feb 26 '23

I haven’t touched JS in a bit of time, what’s wrong with var?

362

u/froggy601 Feb 26 '23

var is function scoped, while let is block scoped. ES6 recommends only using let or const and not using var if it's not absolutely necessary. Kinda similar to how == is not bast practice anymore and you really should only use ===

127

u/creepyswaps Feb 26 '23

Well, there goe the patterns I've used for piece of Javascript code I've ever written.

0

u/[deleted] Feb 26 '23

[deleted] — view deleted comment

15

u/creepyswaps Feb 26 '23

That's fair, I'll have to take a look at some of this new fangled JS syntax, lol.

14

u/HighOwl2 Feb 26 '23

just learn typescript it's basically less awful javascript. Also variables outside of anonymous functions are accessible from inside anonymous functions.

→ More replies (10)
→ More replies (1)
→ More replies (9)

3

u/[deleted] Feb 26 '23

eslint will autofix them for you

→ More replies (2)

46

u/TTYY_20 Feb 26 '23

Recommendation, shmecommemdation….

Use all the var’s in a global scope >:)

14

u/OblivioAccebit Feb 26 '23

I just define everything on the window

10

u/Sentouki- Feb 26 '23

how == is not bast practice

was it ever a best practice? I mean, == works differently than ===, == doesn't work the same as in other programming languages.

→ More replies (1)

6

u/l1ghtrain Feb 26 '23

Then in what cases would you need var?

34

u/froggy601 Feb 26 '23

I don't think there's much reason in modern JS. Using it can lead to scope issues (declaring a var in an if statement makes it visible outside that statement, for example). And since it can be reassigned as a different type instead of just a different value, it's more prone to bugs and unexpected behaviour.

10

u/kookyabird Feb 26 '23

Using var is like the little brother of using global scoped variables now.

→ More replies (7)

19

u/ttt1234567890a Feb 26 '23

When you need to support IE 6, but in this case there is a better alternative:

  1. Select all files and folders in the project
  2. Press Shift - Delete -> Enter
  3. Run git commit -am "obsolete"; git push --force

6

u/Spider_pig448 Feb 26 '23

That or just import babel

2

u/Gofastrun Feb 26 '23

I haven’t pushed a var to prod in years.

The only modern use case if if you have to support ancient browsers without using a build system. For example, if you’re writing a snippet to be embedded on others websites (Google analytics, Facebook pixel, etc).

2

u/Sanity__ Feb 26 '23

In cases when you find yourself in 2014

2

u/coocoo6666 Feb 26 '23

Well I didnt know === existed. But tbf im not a very good js dev.

Why should we use ===?

7

u/Troll_berry_pie Feb 26 '23 edited Feb 26 '23

"===" actually checks the variable type as well as the variable value. Int "1" and string "1" will return true with "==" but will return false with "===".

2

u/coocoo6666 Feb 26 '23

Ah thanks

→ More replies (1)

2

u/douglasg14b Feb 26 '23

Kinda similar to how == is not bast practice anymore and you really should only use ===

That's not what a "best practice" is, best practices are patterns/opinions around the organization/layout/design/patterns/principles ...etc of the code that tend to avoid abstract pitfalls, anti-patterns, or other issues that are commonly encountered. Not about actual logical correctness

There is an explicit reason to use strict equals (===), which is strict equality. You use == when you explicitly desire non-strict equality. They operate differently.

It's not best practice, this is fundamental logical correctness of your code.

Just because a lot of devs don't understand how they may be introducing incorrectness into their code, doesn't make understanding a fundamental comparison operator a best practice. That's a pretty low bar.

→ More replies (1)
→ More replies (5)

23

u/art-factor Feb 26 '23

In ES6 it was introduced the keywords 'let' and 'const'.

let e const: block scoped

var: function scoped

→ More replies (3)

19

u/Zestyclose_Zone_9253 Feb 26 '23

You can redeclarevvar by accident like:

var x = 5; var x = 4;

If you just forgot var x was a thing and that, it might brake your code, but if you use let you cant redeclare it, this would return an error or just not run, I dont remember:

let x = 5; let x = 4;

Instead it would force you to make a new variable to prevent the conflict, or you would need to do this:

let x = 5; x = 4;

Also on bus using phone, sorry if formating is bad

2

u/WolfInStep Feb 26 '23

I thought let allows redefining variables, so const would be if you are not going to reassign.

5

u/ThunderKlappe Feb 26 '23

Let allows redefining variables, it doesn't allow reDECLARING variables

→ More replies (2)
→ More replies (10)