Skip to content

Comment on The WHY of WAT - understanding why 'JavaScript is Crazy'parent

Comments

I can't imagine a single situation where you expect variables used in a sum operation to be arrays/objects. That would be highly confusing.

You always have the option to use

    Number(x) + Number(y)
    (+x) + (+y)
    String(x) + String(y)

"I can't imagine a single situation where you expect variables used in a sum operation to be arrays/objects."

That's precisely the problem. You don't expect them, but there's nothing to prevent it from happening, so it does.

Your proposed solutions don't solve the problem, they occur too late. By the time you're adding together two non-numbers (and non-strings) you've already lost, trying desperately to cast them to numbers first is also wrong, along with so verbose that nobody will ever do it.

Yeah, that was stupid.

What I mean though is I can't imagine code that gives chance for this to happen. Putting an array where a number should be is a very visible mistake.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.