Remix.run Logo
noman-land 9 hours ago

He was pretty close to being NaN% correct.

moritzwarhier 7 hours ago | parent [-]

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.

sheept 4 hours ago | parent | next [-]

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

moritzwarhier 3 hours ago | parent [-]

I intentionally corrected it, because vanilla JS arrays used to behave like this in some contexts, but I'm not even sure about which ones still.

Since the "Array" is a reference to the prototype, I think it might be outdated due to changes in undefined behavior of runtimes when serializing array objects, or logging them.

I'm pretty sure that [object Array] used to be the result of logging an array at some point.

  Object.prototype.toString
always returns the result of

  Array.prototype.join

per spec, afaik, so for an empty array it's the empty string.
jschrf 6 hours ago | parent | prev [-]

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.

moritzwarhier 6 hours ago | parent | next [-]

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 :)

ngruhn 5 hours ago | parent | prev | next [-]

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

- ...

sheept 4 hours ago | parent [-]

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.

crdrost 6 hours ago | parent | prev | next [-]

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

Tade0 5 hours ago | parent | prev [-]

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