What is the ternary operator in JavaScript? - Educative.io

文章推薦指數: 80 %
投票人數:10人

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.



請為這篇文章評分?