Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

ternary operator js

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"}
Source by developer.mozilla.org #
 
PREVIOUS NEXT
Tagged: #ternary #operator #js
ADD COMMENT
Topic
Name
4+7 =