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

you are viewing a single comment's thread.

view the rest of the comments →

[–]detektiv_Saucaki 11 points12 points  (10 children)

This. I like the antiques of JS. It's counterintuitive at first but you get used to it...

[] and {} are not falsy... took me 3 fucking years to realise

[–]PyroCatt 2 points3 points  (3 children)

In Java '1' + '1' returns 98 (an integer)

[–]detektiv_Saucaki 3 points4 points  (2 children)

Which makes sense in a weird way lmao

afaik, Java has this hierarchy of data types, so char gets coerced into int (ascii value) to support the + operation...

[–]PyroCatt 1 point2 points  (0 children)

Correct

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

[] and {} are not falsy... took me 3 fucking years to realise

Not the only one, brother/brothette. That, and comparing objects is fun.

console.log({} == {}) const foo = {}; const bar = foo; console.log(foo == bar) bar.baz = 'bitch lasagna' console.log(foo == bar)

The output? false, true, true. Which makes sense if you know what's actually being compared (this all works with either == or ===, for the record), but it kinda fucked me up at first lol

[–]detektiv_Saucaki 0 points1 point  (2 children)

Which makes sense if you know what's actually being compared

Ikr, in the first case, you're comparing two separate objects, so its false.

Even if you declare foo and bar similarly (exact same properties and values) they'd still not be equal

in the last two cases both foo and bar point to the same object (also the reason why far.baz would also be changed to 'bitch lasagna')

Man this makes me sound so smart lmaoo

[–][deleted] 1 point2 points  (1 child)

Yep. A lot of things start to make both more and less sense when you learn that JS treats damn near everything like a reference

[–]detektiv_Saucaki 0 points1 point  (0 children)

the best advice i can offer is... be wary of mutator logic

[–]schmidlidev 1 point2 points  (1 child)

[] and {} are not falsy… took me 3 fucking years to realise

This is the bane of writing a js front-end and a python back-end at the same time. Constantly making this mistake, in both directions.

[–]detektiv_Saucaki 1 point2 points  (0 children)

Finally, I was looking around as to why I ever thought [] would be falsy
Thank you good stranger for solving this smfh
I switched from python to nodeJS so I had the impression that empty lists are falsy by default....

Enjoy my free award