LennyShort.com

Ternary Operators in JavaScript

This is just some example code I wrote for notetaking:

condition ? expression(truthy) : expression(falsy)

Example Code Snippet:

const exerciseTimeMins = 40
const message = exerciseTimeMins < 30 ? 'You need to try harder!' : 'Doing good!'

Equivalent if statement:

let message = ''

if (exerciseTimeMins < 30) {
    message = 'You need to try harder!'
}
else {
    message = 'Doing good!'
}

If you don't have an "else" statement for your ternary, you can use "null" or an empty string to indicate that the expression should do nothing if the outcome is "falsy".

What I'm reading:

What I've read recently:

What's on my reading list: