Joel Thoms
1 min readAug 10, 2017

--

I think my article would be the counter argument against the ternary being evil.

While a ternary operator and an if statement might appear the same on the surface, there is a big fundamental difference between the two.

An if is a statement and is composed of blocks, while a ternary is an expression.

Blocks are a very imperative way of writing code and my series of articles are written to help fp-beginners transition from an imperative style of code to a more functional style. Eliminating code blocks when possible is one of those small steps.

Wherever a statement is written, a ternary can be written in it’s place. The opposite not true.

Personally, I have found there to be an elegance in the ternary operator and believe it to be underappreciated and underutilized.

const isEven = n => n % 2 === 0
const numbers = [1,2,3,4,5,6,7,8,9,10]
const evenNumbers = numbers.map(isEven)

And while I can’t say that it should replace 100% of if statements, it can definitely replace 99% of them.

--

--

Joel Thoms

Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!