Can I use ternary operators to do something if a condition is true but leave out the false part?


#1

Question

Can I use ternary operators to do something if a condition is true but leave out the false part?

Answer

No, you need something for the ternary operator to do/return if the condition is truthy and something for it to do/return if the condition is falsy. One workaround is to use undefined like so: false ? console.log('prints if true') : undefined.