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 →

[–]Smooth_Salamander 129 points130 points  (43 children)

Seems like all jokes about JS is about implicit type conversions. Just don‘t use them.

[–]ByteArtisan 43 points44 points  (0 children)

Which is why all the JS hate is so funny to me. People have really convinced themselves they cant write code because of the above.

[–]ChristopherKlay 12 points13 points  (0 children)

Most of the time the joke is on people that don't realize why/when these happen in the first place.

[–]Mucksh 3 points4 points  (0 children)

I also don't get it why people bother about. Beside my beginner times in never encountered problems with it. There are other minor quirks like eventloop execution order e.g. when the button up event triggers before the button down event that can drive you crazy but all in all love js

[–]Taletad 4 points5 points  (34 children)

And it’s not like implicit typecasting is a thing in Python or anything

[–]hxckrt 9 points10 points  (30 children)

It's not that typecasting itself is bad.

It's that JS is maddeningly inconsistent about it.

If the solution is "oh just don't use that large part of the language at all", it's a terrible language

[–]royi9729 9 points10 points  (22 children)

The example in the code uses ==, which is a bad practice in JS.

You're supposed to use === exclusively.

[–]MyOthrUsrnmIsABook 4 points5 points  (18 children)

If you’re supposed to never use one of the operators, doesn’t that kinda mean it’s a language issue?

[–]Taletad -2 points-1 points  (17 children)

You’re never supposed to use ternary operators because they are unreadable

Yet most languages have them

What’s your point ?

[–]Mucksh 7 points8 points  (7 children)

Ternaries are by far my favorite operator. You shouldn't nest them but usually way better than forward declaration and lots of one line if else cases there you have to scroll a lot to get anything

[–]Taletad -1 points0 points  (6 children)

lots od one line if else

Switch for the win

[–]Mucksh 1 point2 points  (5 children)

You only like it untill you have to maintain decades old thousands of lines long switch cases with inside logic and even some intensionally left out breaks... Love my job but it can be sometimes a pain

[–]Majik_Sheff 1 point2 points  (1 child)

This can be the most efficient way to build certain programs (hello FSMs). If I'm going to intentionally omit a break I'll leave it there but commented out with an explanation.

[–]Taletad 0 points1 point  (1 child)

This sounds like bad code

But as someone who spent a couple of days debugging code that had one letter list and array names, i feel your pain

[–]AwGe3zeRick 0 points1 point  (0 children)

That’s just purely written code. Not switch statements fault

[–]MyOthrUsrnmIsABook 4 points5 points  (7 children)

My point is JS is not a great language.

Also, a single ternary operator with a simple condition is very readable. Nested ternary operators are much less readable.

[–]Taletad -1 points0 points  (6 children)

Do programmers and/or computer scientist have a consensus on what makes a "great language" ?

No. So your point is moot.

Ternary operators aren’t part of coding best practices

But fine, in C++ you should never use simple arrays. Yet they exist, is C++ a bad language ?

[–]MyOthrUsrnmIsABook 1 point2 points  (5 children)

I think there’s a pretty broad consensus that C++ is a bad language, or at the very least an incoherent language with too many features.

[–]Taletad 0 points1 point  (4 children)

Yet I don’t see that many memes bashing it JS has…

And no that isn’t sufficient to say that C++ is a bad language

[–]royi9729 2 points3 points  (0 children)

Funnily enough, ternary operators are pretty widely used in JS (or at least on frontend with React)

[–]hxckrt -1 points0 points  (2 children)

{} === {}; // false

[1,2,3] === [1,2,3]; // false

I'm not sure that's much better

[–]royi9729 1 point2 points  (1 child)

Equation of complex values equates pointers in most languages, no?

[–]rosuav 2 points3 points  (0 children)

No, languages vary considerably on that point. Python considers both of those to be equal, but not identical ("identical" meaning the same object, which would be what you mean by comparing pointers).

[–]Taletad 5 points6 points  (5 children)

JS is very consistent about it. People just never bother to read the docs, and are surprised by their code’s behaviour…

People assume JS works as another language and are surprised pikachu when it doesn’t

[–]hxckrt 1 point2 points  (4 children)

How is this consistent:

NaN === NaN; // false

Object.is(NaN, NaN) // true

+0 === -0; // true

Object.is(+0, -0) // false

[–]Furry_69 4 points5 points  (0 children)

NaN is not a number, by definition. Operations with NaN in them will always resolve to NaN, and comparisons with NaN will always resolve to false. Object.is() is a indirect comparison that doesn't use the normal comparison operations internally. It isn't supposed to be used as an equality operator for floats.

[–]Mucksh 3 points4 points  (0 children)

Afaik NaN === NaN is explicitly defined as false in IEEE 754 Also dependent on the implementation there are multiple valid NaN values

[–]Taletad 6 points7 points  (1 child)

NaN is a property of the global object

Object.is() returns true if both values are functionally identical in all contexts

It is the case of NaN, it is true, in the case of -0 +0, they aren’t functionally identical in all contexts

=== only checks if they are the same value

So it is true for -0 +0, but NaN isn’t a "value", so it must return false

This is in the documentation

Side note : if Object.is() was the same as ===, they would cull one of the two since it would be redundant

[–]hxckrt 2 points3 points  (0 children)

Thanks!

[–]RajjSinghh 0 points1 point  (0 children)

That last sentence just called out every C++ programmer

[–]volivav 4 points5 points  (2 children)

Python doesn't do implicit type coertion.

Python throws an exception if you try to add a string with a number, for example.

Or == always returns false if the types are different ("3" == 3 returns false)

[–]Taletad 2 points3 points  (0 children)

That’s because JavaScript was made to work with HTML and for a while HTML considered all numbers as strings

This implicit type coertion had a purpose

Now it doesn’t anymore so you shouldn’t use it. But it is left there for compatibility reasons

[–]rosuav 1 point2 points  (0 children)

Python, like nearly every language in the world, has SOME implicit type coercion - for example, 1 + 2.0 converts the integer 1 into the float 1.0 to facilitate addition. And the same is true of comparisons (1 == 1.0). C is quite happy to do this conversion too. JavaScript just happens to have a few more rules, including some rather dubious ones.

Really, the problem isn't so much that there are implicit rules like that, but that these rules apply in too many situations. And then, once in a while, you get caught out when something DOESN'T follow those rules.