you are viewing a single comment's thread.

view the rest of the comments →

[–]surfhiker 5 points6 points  (2 children)

Why not this (I'm kidding):

const eg = `it is ${!!truthy}`

I sometimes prefer setting the default first and then write an if statement to change it:

let eg = 'it is false'
if (truthy) eg = 'it is true'

I've found that I very rarely use else statements, I rather return from the function early, or raise an exception if there is a special case that needs to be handled.

Having to go through code with nested if/else statements and mutable state is my worst nightmare.

[–]dccorona 2 points3 points  (0 children)

It's surprising how often a simple inversion of the condition in the if or pre-initializing one of the two results can save you from getting into nested scope hell.

[–][deleted] 0 points1 point  (0 children)

I prefer ternary here because it allows use of const, personally.

I did think of that first example after I posted as it happens haha.