To simplify the if...else statement we can use a short-hand syntax which is known as ternary operator.
Example:
let isNightTime =true;if (isNightTime) {console.log('Turn on the lights.');} else {console.log('Turn off the lights.');}// Output: Turn on the lights.
Above if...else code block can written as down below:
Example:
isNightTime ?console.log('Turn on the lights.') :console.log('Turn off the lights.');// Output: Turn on the lights.
Example:
let isLocked =false;isLocked ?console.log('You will need a key to open the door.') :console.log('You will not need a key to open the door.');let isCorrect =true;isCorrect ?console.log('Correct!') :console.log('Incorrect!');let favoritePhrase ='Love That!';favoritePhrase ==='Love That!'?console.log('I love that!') :console.log("I don't love that!");// Output: You will not need a key to open the door.// Correct!// I love that!