if else in one line javascript code example

Example 1: if statement javascript one line ?

(lemons) ? alert("please give me a lemonade") : alert("then give me a beer");

Example 2: how to make one line if in js

// If statement
// if (condition) //code
if (5 > 3) return "Is greater"; // "Is greater"

// If else statement
// (condition) ? if : else
let res = (1 > 3) ? "is greater" : "is less than";// "is less than"

Example 3: one line if else if statement javascript

var variable = (condition) ? (true block) : ((condition2) ? (true block2) : (else block2))

Example 4: one line if statement javascript

if (lemons) document.write("foo gave me a bar");

Example 5: javascript if one line

/* When width is <= 600 breakpoint is 1, when width is between 600 and 1000 breakpoint is 2, else is 4 */
let breakpoint = width <= 600 ? 1 : width <= 1000 && width > 600 ? 2 : 4; // Breakpoint observer code

Tags:

Misc Example