? 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 thisif(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 bracketslet condition &&'show this string if condition is true';
// ternary operator in javascriptconst x =6;let answer = x >10?"greater than 10":"less than 10";console.log(answer);// output: less than 10// nested conditionconst 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>functiongfg(){//JavaScript to illustrate //Conditional operator letPMarks=40let result =(PMarks>39)?"Pass":"Fail";//Syntax:- (condition) ? do this if true : do this if falsedocument.write(result);}gfg();</script>
// Write your function here:constlifePhase=(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 =-2let b =-4let c =1let d =0let e =10let 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 ?trueCondition:falseCondition9<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'.Ifthis 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 aslet 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 aslet startingNum = startingNum && otherNum
functionfee(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*/
let a,b,c,d =[(3>1),(2>2),(4>2),(19>22),(19>22)]let ans = a?
b?
a==d?'yes1':'no1':c==a?
d?'malaki si b':'maliit lang daw':'eto ung sagot 8787':d==b?'yes3':'no3'console.log(ans)