All the Crappy Javascript

Javascript. One of the idioms I love in Javascript land is being able to do this:

And now something is the result of DefTrue() … A lot of libraries (and people also tend to write functions like this too:

The fact that these people (and I won’t call them out) check to make sure the things exist before they actually return the function is a Good Thing™. I just wish they checked it properly, because falsey is not exactly the same as existing.

For example, I could totally screw this up by accident:

How long would it take you to find this bug? Because the error wouldn’t appear anywhere near this file. In fact, it might only intermittently appear many, many, many callbacks later after being passed around and around. I’ve had to chase down three of these exact bugs in the last couple of months … buried deep, deep in library code.

  1. I had a bug where an api response was a literal empty string (falsey) which killed the app.
  2. I had two bugs where the function was changed to return true and it was originally returning a callback. Like above, it magically kept working until that “callback” was called, minutes later after being set.

This is why Javascript is crappy.

It’s because we’re human and Javascript doesn’t care. Our customers do. So do our paychecks.

You can type check in Javascript. Do it when it’s feasible. !X is not an existence check, things can exist in a falsey state. undefined means it literally doesn’t exist. So check that.

For the love of God, the universe, Alan Turing, and Linus Torvalds … falsey IS A VALUE and IT DOES EXIST.


Want an inside scoop?

Check out PHP Shenanigans for only €5/mo or €30/yr