Conditional (ternary) operator - JavaScript - W3cubDocs
文章推薦指數: 80 %
The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ) ... Conditional(ternary)operator Theconditional(ternary)operatoristheonlyJavaScriptoperatorthattakesthreeoperands:aconditionfollowedbyaquestionmark(?),thenanexpressiontoexecuteiftheconditionistruthyfollowedbyacolon(:),andfinallytheexpressiontoexecuteiftheconditionisfalsy.Thisoperatorisfrequentlyusedasanalternativetoanif...elsestatement. Syntax condition?exprIfTrue:exprIfFalse Parameters conditionAnexpressionwhosevalueisusedasacondition.exprIfTrueAnexpressionwhichisevaluatediftheconditionevaluatestoatruthyvalue(onewhichequalsorcanbeconvertedtotrue).exprIfFalseAnexpressionwhichisexecutediftheconditionisfalsy(thatis,hasavaluewhichcanbeconvertedtofalse). Description Besidesfalse,possiblefalsyexpressionsare:null,NaN,0,theemptystring(""),andundefined.Ifconditionisanyofthese,theresultoftheconditionalexpressionwillbetheresultofexecutingtheexpressionexprIfFalse. Examples Asimpleexample varage=26; varbeverage=(age>=21)?"Beer":"Juice"; console.log(beverage);//"Beer" Handlingnullvalues Onecommonusageistohandleavaluethatmaybenull:letgreeting=person=>{ letname=person?person.name:`stranger` return`Howdy,${name}` } console.log(greeting({name:`Alice`}));//"Howdy,Alice" console.log(greeting(null));//"Howdy,stranger" Conditionalchains Theternaryoperatorisright-associative,whichmeansitcanbe"chained"inthefollowingway,similartoanif…elseif…elseif…elsechain:functionexample(…){ returncondition1?value1 :condition2?value2 :condition3?value3 :value4; } //Equivalentto: functionexample(…){ if(condition1){returnvalue1;} elseif(condition2){returnvalue2;} elseif(condition3){returnvalue3;} else{returnvalue4;} } Specifications Specification ECMAScriptLanguageSpecification(ECMAScript)#sec-conditional-operator Browsercompatibility Desktop Mobile Server Chrome Edge Firefox InternetExplorer Opera Safari WebViewAndroid ChromeAndroid FirefoxforAndroid OperaAndroid SafarionIOS SamsungInternet Deno Node.js Conditional_Operator 1 12 1 3 3 1 1 18 4 10.1 1 1.0 1.0 0.10.0 Seealso ifstatementNullishcoalescingoperatorOptionalchainingMakingdecisionsinyourcode—conditionalsExpressionsandoperators ©2005–2021MDNcontributors.LicensedundertheCreativeCommonsAttribution-ShareAlikeLicensev2.5orlater. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
延伸文章資訊
- 1條件運算子- JavaScript
JavaScript Demo: Expressions - Conditional operator. xxxxxxxxxx. 1. function getFee(isMember) {. ...
- 2Conditional (ternary) operator - JavaScript - W3cubDocs
The conditional (ternary) operator is the only JavaScript operator that takes three operands: a c...
- 3Guide to the Ternary Operator in JavaScript - Stack Abuse
A ternary operator is a three-operand operator that is supported in most programming languages, i...
- 4Conditional Operator in C - Javatpoint
- 5Conditional Operator - JavaScript - w3resource
The conditional operator is used as a shortcut for standard if statement. It takes three operands...