? called ternary operator is shorcut for, "if statement".
// below we make object with key id, and assign it a value from body.id.
// if body.id is null it will return false and we assign it res.id.
{ id: body.id? body.id : res.id }
// we can write it in if statement like this
if(body.id){
return {id:body.id}
}else{
return {id:res.id}
}
condition ? exprIfTrue : exprIfFalse
////////////////////////////////////
//Example
const age = 26;
//set the bevarage conditionally depending on the age
const beverage = age >= 21 ? "You can have a Beer" : "Stick to Juice Kid";
console.log(beverage); //OUTPUT: "You can have a Beer"
//Same as
//if(age>=21){
//bevrage="You can have a Beer"}
//else{
//bevrage = "Stick to Juice Kid"}
var variable;
if (condition)
variable = "something";
else
variable = "something else";
//is the same as:
var variable = condition ? "something" : "something else";
let showme || "if the variable showme has nothing inside show this string";
let string = condition ? 'true' : 'false'; // if condition is more than one enclose in brackets
let condition && 'show this string if condition is true';
// ternary operator in javascript
const x = 6;
let answer = x > 10 ? "greater than 10" : "less than 10";
console.log(answer);
// output: less than 10
// nested condition
const answer = x > 10 ? "greater than 10" : x < 5 ? "less than 5" : "between 5 and 10";
console.log(answer);
// output: between 5 and 10
// Syntax
condition ? ifTrue : ifFalse
<script>
function gfg() {
//JavaScript to illustrate
//Conditional operator
let PMarks = 40
let result = (PMarks > 39)?
"Pass":"Fail"; //Syntax:- (condition) ? do this if true : do this if false
document.write(result);
}
gfg();
</script>
// Write your function here:
const lifePhase = (age) => {
return age < 0 || age > 140 ? 'This is not a valid age':
age < 3 ? 'baby':
age < 13 ? 'child':
age < 20 ? 'teen':
age < 65 ? 'adult':'senior citizen';
}
console.log(lifePhase(5))
let a = -2
let b = -4
let c = 1
let d = 0
let e = 10
let ans = a>b?
a>c?
a>d?'yes1':'no1'
:b>c? 'yes2' : 'no2':
e> b? 'mas malaki' : 'maliit daw'
c>d?'yes3': 'no3'
console.log(ans)
//if there is complex statement use if statement
//otherwise ternary operator will be best to use.
condition ? true Condition : false Condition
9<10 ? "small":"bigger" // small code run
let memberType = 'basic';
let price = memberType === 'basic' ? 5 : 10;
In the example, the condition that is evaluated is whether memberType === 'basic'.
If this condition is true, then price will be 5, and otherwise it will be 10.
The equivalent long-hand conditional expression would be:
let memberType = 'basic';
let price;
if (memberType === 'basic') {
price = 5;
} else {
price = 10;
}
let startingNum = startingNum ? otherNum : 1
// can be expressed as
let startingNum = otherNum || 1
// Another scenario not covered here is if you want the value
// to return false when not matched.
//The JavaScript shorthandfor this is:
let startingNum = startingNum ? otherNum : 0
// But it can be expressed as
let startingNum = startingNum && otherNum
function fee(isMember)
{
return isMember==true?"$20":"$100"
}
console.log(fee(true))
/*a==b?c:d*/
/*the syntax is check if a equals b, if so, return c otherwise return d*/
/*1==1?4:10: check if 1 equals 1, if so, return 4 otherwise return 10*/