if statement js shorthand code example

Example 1: javascript if shorthand

condition ? doThisIfTrue : doThisIfFalse

1 > 2 ? console.log(true) : console.log(false)
// returns false

Example 2: shorthand if in javascript with return

pet = pets.find(pet => pet.type ==='Dog' && pet.name === 'Tommy');
console.log(pet); // { type: 'Dog', name: 'Tommy' }

Example 3: ternary operator javascript

condition ? exprIfTrue : exprIfFalse

//Example
let age = 26;
let beverage = (age >= 21) ? "Beer" : "Juice";
console.log(beverage); // "Beer"