Home

Multiple Line "If" Statement in CoffeeScript

There are lots of different ways to move "if" statements in CoffeeScript into multiple lines. Pick the one you like the best!

Creating if statements that span multiple lines in CoffeeScript can be hard to remember, but it's quite easy. The trick is to make sure you

  • keep your indentation the same, and
  • end a line with an operator or a backslash

Simple Examples

Each of these examples will work, but they aren't necessarily pretty.

1. Backslash

Use a backslash to continue the line:

if a == 1 && b == 1 \
|| c == 1 && d == 1
doSomething()

2. "Or" Operator

Use an "or" operator at the end of a line:

if a == 1 && b == 1 ||
c == 1 && d == 1
doSomething()

3. Parentheses

Use parentheses (and an "or" operator):

if(
a == 1 && b == 1 ||
c == 1 && d == 1
)
doSomething()

Refactoring

Let's do a bit of refactoring to see how we can make these a little cleaner.

1. Whitespace

Although it may seem odd, the indentation in CoffeeScript is looking for two spaces for it subsequent line. So if you use more than two, it will work as a continuation of the previous line. For example:

if a == 1 && b == 1 ||
c == 1 && d == 1
doSomething()

2. Parentheses

If that's hard to read, you can include parentheses.

if(a == 1 && b == 1 ||
c == 1 && d == 1)
doSomething()

3. Break Up Logic

Or, you could break up the logic into multiple statements.

e = a == 1 && b == 1
f = c == 1 && d == 1
if e && f
doSomething()

Note: I prefer symbols for operators, such as && vs. and as I actually find it easier to read, even though it's less semantic. You can substitute my && for and and || for or and achieve the same effect.


References:

Let's Connect

Keep Reading

Using jQuery and CoffeeScript in WordPress

The standard ready function doesn't always work in WordPress plugins. Here's a workaround.

May 01, 2013

Deleted 3 Apps Thanks to Raycast

Raycast has boosted my productivity while also covering the offerings of three applications I was using.

Dec 09, 2022

Simplify Components by Separating Logic from Presentation using Adapters

It's tough to know when it's the right time to break a component up into smaller components. Here's a way to approach that process that relies on more than what you see on the screen.

Jul 10, 2020