What is the ternary operator in JavaScript? - Educative.io
文章推薦指數: 80 %
The conditional ternary operator assigns a value to a variable based on some condition. It is used in place of the if statement. The ternary operator is the ... SolutionsEducativeEnterpriseEnablementplatformDevelopersLearnnewtechnologiesProductsCoursesforEnterpriseSuperchargeyourengineeringteamCoursesforIndividualsWorldclasscoursesOnboardingOnboardnewhiresfasterAssessmentsMeasureyourSkillScorePersonalizedLearningPlansPersonalizedPlansforyourgoalsProjectsBuildrealworldapplicationsAnswersTrustedAnswerstoDeveloperQuestionsPricingForEnterpriseTailoredforyourteamForIndividualsStayaheadofthecurveCoursesLogInJoin forfreeTrustedanswerstodeveloperquestionsTrustedAnswerstoDeveloperQuestionsRELATEDTAGSternaryjavascriptoperatorWhatistheternaryoperatorinJavaScript?EducativeAnswersTeamTheconditionalternaryoperatorassignsavaluetoavariablebasedonsomecondition.Itisusedinplaceoftheifstatement. TheternaryoperatoristheonlyJavaScriptoperatorthattakes3operators. Syntax Theconditionistheexpressionwhosevaluedetermineswhichvalueisused. Thevalueafterthe?isreturnediftheconditionreturnstrue. Thevalueafterthe:isreturnediftheconditionreturnsfalse. Example ThecodebelowusestheternaryoperatortoshowasimpleIfelseconditiontestingcode,anditsequivalentcode. If_ElseTernaryvargrade; varmarks=43; if(marks<30){ grade='Fail'; } else{ grade='Pass'; } console.log(grade)RunMultipleTernaryoperators Ifwewanttocheckmultiplevariationsofthesametestcase,wecanusemultipleelseifstatements.Thecodebelowshowshowthesemultiplevariantscanbeimplementedthroughbothelseifandternaryoperators. If_ElseTernaryvargrade; varmarks=68; if(marks<30){ grade='Fail'; } elseif(marks<60){ grade='Pass' } elseif(marks<90){ grade='Distinction' } else{ grade='HighDistinction'; } console.log(grade)RunMultipleOperations Wecanalsoperformmorethanoneoperationforaternaryoperator,butthecommandsmustbeseparatedwithcommas.First,thecodebelowsetsthevalueofthegradevariable.Then,ifthecorrespondingconditionisfulfilled,itprintsamessagewiththatvariable. varmarks=43 vargrade; (marks<30)?(grade='Fail',console.log("Betterlucknexttime")) :(grade='Pass',console.log("Congratulations")); console.log(grade)Run RELATEDTAGSternaryjavascriptoperatorCopyright©2022Educative,Inc.AllrightsreservedRELATEDCOURSESViewallCoursesKeepExploringRelatedCoursesLearnin-demandtechskillsinhalfthetimeSOLUTIONSEducativeforEnterpriseEducativeforIndividualsEducativeforHR/recruitingEducativeforBootcampsPRODUCTSEducativeLearningEducativeOnboardingEducativeSkillAssessmentsPricingForEnterpriseForIndividualsRESOURCESEducativeBlogEducativeAnswersEMHubCONTRIBUTEBecomeanAuthorBecomeanAffiliateBecomeaContributorLEGALPrivacyPolicyCookieSettingsTermsofServiceBusinessTermsofServiceABOUTUSOurTeamCareersHiringMORECourseCatalogEarlyAccessCoursesFreeTrialsEarnReferralCreditsCodingInterview.comPressContactUsSOLUTIONSEducativeforEnterpriseEducativeforIndividualsEducativeforHR/recruitingEducativeforBootcampsPricingForEnterpriseForIndividualsLEGALPrivacyPolicyCookieSettingsTermsofServiceBusinessTermsofServicePRODUCTSEducativeLearningEducativeOnboardingEducativeSkillAssessmentsCONTRIBUTEBecomeanAuthorBecomeanAffiliateBecomeaContributorABOUTUSOurTeamCareersHiringRESOURCESEducativeBlogEducativeAnswersEMHubMORECourseCatalogEarlyAccessCoursesFreeTrialsEarnReferralCreditsCodingInterview.comPressContactUsSOLUTIONSEducativeforEnterpriseEducativeforIndividualsEducativeforHR/recruitingEducativeforBootcampsRESOURCESEducativeBlogEducativeAnswersEMHubCONTRIBUTEBecomeanAuthorBecomeanAffiliateBecomeaContributorABOUTUSOurTeamCareersHiringPRODUCTSEducativeLearningEducativeOnboardingEducativeSkillAssessmentsPricingForEnterpriseForIndividualsLEGALPrivacyPolicyCookieSettingsTermsofServiceBusinessTermsofServiceMORECourseCatalogEarlyAccessCoursesFreeTrialsEarnReferralCreditsCodingInterview.comPressContactUsCopyright©2022Educative,Inc.Allrightsreserved.
延伸文章資訊
- 1JavaScript Ternary Operator (with Examples) - Programiz
A ternary operator evaluates a condition and executes a block of code based on the condition. Its...
- 2Ternary Operator - Beau teaches JavaScript - YouTube
- 3How do you use the ? : (conditional) operator in JavaScript?
The conditional (ternary) operator is the only JavaScript operator that takes three operands. Thi...
- 4Guide to the Ternary Operator in JavaScript - Stack Abuse
A ternary operator is a three-operand operator that is supported in most programming languages, i...
- 5Ternary Operator in JavaScript - Scaler Topics
A Ternary Operator in Javascript is a special operator which accepts three operands. · It is also...