Snippet

Opérateurs ternaires js

4 réponses de la communauté

condition ? doThisIfTrue : doThisIfFalse

1 > 2 ? console.log(true) : console.log(false)
// returns false
let amount = 50;
let food = amount > 100 ? 'buy coka-cola' : 'buy just a water bottle';
console.log(food)
// 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
FullName: (obj.FirstName && obj.LastName) ? obj.FirstName + " " + obj.LastName : "missing values",