Question mark (?) paired with colon ( : ) is a ternary operator in javascript.
condition ? code A : code B
As name suggest, ternary operator syntax has three operands.
First is a condition followed by a question mark (?). We test this condition, if it is true or false.
If the condition is true, code A is run.
If the condition is false, code B is run.
Ternary operator let’s you write if..else statement in a single line of code.
if..else statement
let superhero = ‘goku’;
if(superhero === ‘goku’){
console.log(‘anime’);
}else{
console.log(‘comic’);
}
Ternary operator
let superhero = ‘goku’;
let comic = superhero === “goku” ? ‘anime’ : ‘comic’;