You don’t actually NEED if statements (ever)
Sure they’re a nice and easy way to create control flow, but you can write many billions of lines of conditional JS code without a SINGLE if statement.
And there are many situations where a different construct shows what you wanna do way more clearly — something we can’t ignore as long we write code for humans.
Not to mention lower verbosity and shorter code…
So: let’s look at some powerful if statement upgrades📈.
1. The AND (&&
) operator
The &&
operator, unique to JavaScript.
With it I quickly go from this:
To this:
I’ve eradicated the nested and compacted the branching logic into a one-liner.
You want to use this when there’s an if
but no matching else
; especially when the if
block has only one line.
Even if there are multiple lines you can abstract them into a separate function and apply…