upvote
He was pretty close to being NaN% correct.
reply
To be fair there were a couple of disasters, such as [object Array] and undefined.

Feels like the world is hanging on a single thread by now.

reply
You probably meant [object Object] :) Since arrays have their own default toString implementation (its own can of worms that is the basis for JSFuck[0]), you'd have to go out of your way with Object.prototype.toString.call to get [object Array]

[0]: https://jsfuck.com/ relies on Array#toString for casting values to strings

reply
Protip: never even mention undefined in your codebase. Erase it from your vernacular. If you ever need to pass or return nothing, you use null.
reply
I do it this way, but this led to people asking me in reviews why I use NULL^^

My explanation was that it signals intent to me, and is different from some property not being part of the expected object shape or not having been initialized because of some accident or logic failure.

Since then, I've sticked to it, and am "allowed" to use NULL ^^

It can lead to some annoying checks in TS for primitively-typed properties, so for these, I still allow explicit usage of undefined when it's simpler given the surrounding code.

But I agree with you in principle. Using "undefined" as a "second nullish value" and explicitly checking for it is a programming error.

When there's object/areay vs null/undefined, thankfully, the truthiness narrowing often allows me to interface with code relying on "undefined" without explicitly handling this "value" in my own parts of the code base :)

reply
If you're a type purist then undefined looks nicer... It'd be like being upset at booleans because things get coerced to them or some 'ish.

No: it's the type that has one inhabitant—it's not that type's fault that it appears as a default argument or var or was left out of JSON... So long as typeof null === "object", null is the absurd one

reply
You can't get rid of undefined. You can't ban null either. They're both used in the core language all over the place:

undefined camp:

- out of bounds array index access [1,2,3][42]

- non existent object key access {}.foo

- array.find(...) no result

- ...

null camp:

- document.querySelector(...) no result

- regex.match(...) no result

- ...

reply
In general the language uses `null` if it would otherwise return an object (similar to Java; this is also why null is an object but not really in both languages), while `undefined` is used if it could otherwise return any value.

Could the language have done without both null and undefined? Definitely. But it's here and here to stay.

Though, it's not the only language with two nulls. Julia has nothing and missing, though their semantics are more well defined and with different behaviors than in JavaScript.

reply
null is of type 'object' though, while undefined is undefined - way better to have a separate type.
reply