how to use ternary operator in javascript
condition ? ifTrue : ifFalse//ternary operator syntax and usage:
condition ? doThisIfTrue : doThisIfFalse
//Simple example:
let num1 = 1;
let num2 = 2;
num1 < num2 ? console.log("True") : console.log("False");
// => "True"
//Reverse it with greater than ( > ):
num1 > num2 ? console.log("True") : console.log("False");
// => "False"var variable;
if (condition)
variable = "something";
else
variable = "something else";
//is the same as:
var variable = condition ? "something" : "something else";condition ? doThisIfTrue : doThisIfFalse
1 > 2 ? console.log(true) : console.log(false)
// returns falsefunction example(…) {
return condition1 ? value1
: condition2 ? value2
: condition3 ? value3
: value4;
}
// Equivalent to:
function example(…) {
if (condition1) { return value1; }
else if (condition2) { return value2; }
else if (condition3) { return value3; }
else { return value4; }
}
var age = 26;
var beverage = (age >= 21) ? "Beer" : "Juice";
console.log(beverage); // "Beer"
Also in JavaScript:
- js reverse
- how to make a javascript game
- if statements javascript
- how react work
- javascript function call with variable
- best js ide
- telerik asp.net ajax error creating control
- js get last 4 digits
- javascript add query string to url
- javascript associative array
- underscore js check boolean value
- .append js
- angular tab change smooth transition
- update cart quantity javascript
- javascript remove spaces at the beginning of the end of the string
- razor list to js array
- how to setstate in parent in child component in react native
- reqeuest body in hapijs
- jquery for each tr in td
- odd or even js
- tolowercase javascript
- json placholder
- tappify.js
- create 5 car object using a constructor function in javascript