Guard clauses in Javascript

I first came across the idea of guard clauses in Ruby. The concept is a fairly straightforward one: instead of wrapping a block in an if statement, you exit on a single line, keeping the proceeding code at the same indentation level.

In a language like Javascript, there’s no end-benefit to this style of exiting. There is, in my opinion, a benefit for anyone touching your codebase however: it improves readability and just feels nicer; cleaner.

An example then. Traditionally, you might stop certain code running like this.

1const assertString = (str) => {
2 if (typeof str === 'string') {
3 return true
4 } else {
5 return false
6 }

Using a guard clause, you would write the above like so.

1const assertString = (str) => {
2 if (typeof str !== 'string') { return false }
3 return true

I’ve adopted this style of exiting everywhere applicable and really like it. I’d encourage you to give it a whirl.