Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

ternary operator javascript

// ternary operator in javascript
const x = 6;
let answer = x > 10 ? "greater than 10" : "less than 10";
console.log(answer);
// output: less than 10

// nested condition
const 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
Source by developer.mozilla.org #
 
PREVIOUS NEXT
Tagged: #ternary #operator #javascript
ADD COMMENT
Topic
Name
1+2 =