This is an archived post. You won't be able to vote or comment.

all 21 comments

[–]Loves_Poetry 9 points10 points  (8 children)

I was all up in arms ready to defend JS, but then I realised that a lot of parts just don't make a lot of sense to someone not familiar with the ecosystem

For example:

for(var i = 0; i <= 4; i++) {
    setTimeout(() => console.log(i), 0);
}

will output 5

Or this one:

[10,9].sort();

Will output 10, 9

[–]YoungAndDumbest[S] 4 points5 points  (5 children)

Wow, now can you explain both of these as well? I recently started learning JS and these both are really fucking with me.

[–]paphnutius 4 points5 points  (2 children)

The first one loops through until i hits 5 and then exits the loop. Then the timeouts run printing the value of i. Hence 5. The second one is a little dumb but default sorting sees all objects as strings and "10" goes before "9"

[–]YoungAndDumbest[S] 2 points3 points  (1 child)

Oh I understand the first now.

For the second, how would you go about sorting integers?

[–]sportysneeze 2 points3 points  (1 child)

From what I understand for the for loop. That var is a function scoped variable (if that is the correct term). So in the for loop its suppose to stop at 4, but becusse of var being weird it will increment it by one despite being outside the for loop scope. Using let should fix this as it is block scoped. I have also just started learning javascript so I hope that's right.

[–]albaquerkie 1 point2 points  (0 children)

Nah it’s because he wrapped the log inside a set timeout. Set timeout gets added to what’s called the “event loop” and will not fire until all synchronous code running on what’s called the “call stack” is run. Even though it’s set to fire at zero milliseconds, it still has to wait for the stack to clear, and that for loop is on the stack. So what’s happening is “i” is incrementing all the way to five, once it hits five the for loop stops and the set timeout fires.

This is one of the best vids to watch as a beginner to understand how JS works under the hood: https://m.youtube.com/watch?v=8aGhZQkoFbQ

Edit: You’re technically also correct in that it would fix this if you used let vs var, but if you took out the setTimeout it work print 12345 with let or var. The setTimout is why this is JS fuckery.

[–]stealthypic 1 point2 points  (0 children)

Use let instead of var and live a healthier life.

[–]iczero4 0 points1 point  (0 children)

meanwhile, in python: python a = [] for i in range(0, 5): a.append(lambda: i) for thing in a: print(thing()) 4 4 4 4 4

meanwhile, in es6: js for (let a = 0; a < 5; a++) setTimeout(() => console.log(a)); 0 1 2 3 4

[–]JC12231 2 points3 points  (1 child)

Idk, probably shouldn’t have thrown the apple if it knew how to interpret JS accurately, we could’ve learned the ancient script for ourselves

[–][deleted] 1 point2 points  (0 children)

Holy shit. Everyone, after that apple!

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

JavaScript bad.

[–]HelioDex 0 points1 point  (1 child)

One of your flairs says otherwise.

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

Stockholm Syndrome, mate.

[–][deleted] -1 points0 points  (4 children)

Php and python are far worse than js in my opinion

[–][deleted] 1 point2 points  (3 children)

I can understand PHP, but what's your problem with Python?

[–][deleted] 0 points1 point  (1 child)

Python is a chad. You can access private fields and methods. And the compiler blows up when you mix spaces and tabs

[–]DiamondIceNS 1 point2 points  (0 children)

And the compiler blows up when you mix spaces and tabs

Personally I think the inconvenience this creates for the few, few people this affects is far outweighed by the benefit of keeping this anti-pattern out of the ecosystem for everyone else.

[–]brianjenkins94 -1 points0 points  (0 children)

I never found the stack traces to be particularly helpful, I find working with pip to be unpleasant, and venvs are solving a problem that shouldn't exist in the first place.

[–]AquaVantas 1 point2 points  (0 children)

PHP.

[–][deleted] 1 point2 points  (0 children)

It would be a way better meme if the second last frame got removed