DEV Community

Discussion on: What is the oddest JavaScript behavior?

deleteman123 profile image
Fernando Doglio

Wouldn't you say that most of the "strange" behavior is actually associated with people not caring enough to read the actual specs? I mean, the example you listed is perfectly explained in the answers. It's not strange, it behaves as expected if you read the specs.
Same goes for a lot of the comments in this thread, especially those related to data types. If I write a spec for a new language saying that:

typeof 1 == number
typeof 2 == string

No matter how crazy that looks from the outside, if my language's inner logic makes sense, then it's completely valid and not crazy.

I guess what I'm trying to say here is that people need to start seeing JavaScript for the very special boy it is and stop trying to compare it with the rest of the class. It's different, but we love it anyway (at least I know I do :P)

lepinekong profile image
lepinekong • Edited on

From Deming / System Thinking:

A system must have an aim. What's the aim ;) Making the right things (good spec relative to that aim) is far more important than doing the things right (respect a spec) and that's all the problem in software industry in general: the problem is more about how to do the right product (so spec) than to do a product that respects a spec but people won't buy ;)

Doing a spec for a programming language often targets the machine first instead of human first, that should be reverse ;)

camdhall profile image

I don't really agree with this at all. Most of my experience is in JavaScript and I've read most of the specs if not all, at one time or another. It doesn't mean these things make "sense". Just because I know WHY it does dumb stuff, doesn't mean it's not dumb. Like, I know why people would try to rob a bank. Doesn't mean it's a perfectly rationale thing to do.

deleteman123 profile image
Fernando Doglio

Well, the concept of "dumb" requires a context and to be compared against similar actions with different results. Thus you are taking into account expected behavior from others, even if you don't do it consciously. It's the same with your example: robbing a bank is bad, even if you know why they do it, because, in your context, you follow the law but then again, your context isn't the same as that of the robbers.
That is all I'm trying to say, the context here is everything. If we say JS's behavior is "dumb" because in other languages typeof NaN (or its equivalent) should never be "number", then we're taking things out of context. The ECMAScript standard states that Numbers should be IEEE-754 floating point data. This includes Infinity, -Infinity, and also NaN.

I don't mean to start a flame war here, so I hope I'm not offending anyone, all I'm just saying is that before claiming JS is stupid, dumb or behaving strangly, we should consider what we're comparing it against. That's all.

Thread Thread
camdhall profile image
CamDHall • Edited on

I started programming in JS. I spent probably 5 or 6 years working only with JS. But even before I started working with C# and C++, I thought the way JS handles things was clearly not the best way of handling them.

I can go further on the banking example. Even if I didn't follow the law, robbing a bank is not a smart move. There's easier targets. Less risky targets. Probably even targets that pay all most as well, with far less chance of being caught. So it's still pretty stupid, even in the context of being a criminal. I don't know a lot of thiefs, but I suspect if you ask a bunch of thiefs if robbing a bank is a good idea, they'd tell you no. Rob some poor guy leaving the atm or something.

JavaScript could definitely handle types better. Among other things. Even in the context of what it's used for. Typescript is maybe a good example of this.

I'm also not being malicious or anything. I Like JS, it's still what most of my side projects are in, even though I could choose to use something else. But it has problems. Some of which are self imposed.