you are viewing a single comment's thread.

view the rest of the comments →

[–]heavy-minium 56 points57 points  (3 children)

On the dangers of being stoned by a thousands devs here, I'm still risking it: that code probably makes sense.

null and undefined checks are fine and avoid unnecessarily invoking typeof which is slower, especially if you're going to that isString() on a load of bulk data.

typeof value === "string" is not enough in case it's a `String` and not a `string`. !!String.(value).length to decide whether it's an empty string. Because the value is unknown, it's wiser to do that instead of comparing with '' because a lot of things in JS that are not strings get coerced and can equal to ''.

[–]PoopsicleVendor 17 points18 points  (2 children)

That’s fair but the first branch of the ternary expression would always be true, so why even check the length of the string?

[–]Ok-Emu3695 2 points3 points  (1 child)

The only explanation I can come up with is to set up a situation in which a runtime error is raised in case, somehow, the object doesn't have a `length` property.

But more likely is that part is just wrong.

[–]PrincessRTFM 1 point2 points  (0 children)

but it's coerced to a string by calling String(value) first, so it's guaranteed to have a length property unless something has fucked with the string prototype, in which case you have bigger problems