ternary operator javascript multiple statements code example

Example 1: Use Multiple Conditional Ternary Operators Javascript

// Use Multiple Conditional Ternary Operators Javascript.


function checkSign(num) {

	return num > 0 ? "positive" : num < 0 ? "negative" : "zero";

}

console.log(checkSign(10));
console.log(checkSign(-10));
console.log(checkSign(0));

Example 2: ternary function javascript

var variable;
if (condition)
  variable = "something";
else
  variable = "something else";

//is the same as:

var variable = condition ? "something" : "something else";

Example 3: multiple ternary operator javascript

var icon = (area == 1) ? icon1 : (area == 2) ? icon2 : icon0;

Example 4: The conditional (ternary) operator with three condition

String year = credits < 30 ? "freshman" : credits <= 59 ? "sophomore" : credits <= 89 ? "junior" : "senior";