“Operator terner bersyarat” Kode Jawaban

Operator kondisional (ternary) dengan tiga kondisi

String year = credits < 30 ? "freshman" : credits <= 59 ? "sophomore" : credits <= 89 ? "junior" : "senior";
Lazy Louse

Operator ternary

condition ? expression-if-true : expression-if-false;

function findGreater(a, b) {
  return a > b ? "a is greater" : "b is greater";
}
Owlthegentleman

Operator terner bersyarat

function getFee(isMember) {
  return (isMember ? '$2.00' : '$10.00');
}

console.log(getFee(true));
// expected output: "$2.00"

console.log(getFee(false));
// expected output: "$10.00"

console.log(getFee(null));
// expected output: "$10.00"
omeatai

Parameter fungsi operator bersyarat (ternary)

serveDrink(userIsYoungerThan4 ? 'Milk' : userIsYoungerThan21 ? 'Grape Juice' : 'Wine');
Lovely Lizard

Gunakan operator bersyarat (ternary)

function checkEqual(a, b)
{
return a === b ? “Equal” : “Not Equal” ;
}
checkEqual(1, 2);
Terrible Trout

Parameter fungsi operator bersyarat (ternary)

//Instead of:
var welcomeMessage  = 'Hello ' + (username ? username : 'guest');

//Can use:
var welcomeMessage  = 'Hello ' + (username || 'guest');
Lovely Lizard

Jawaban yang mirip dengan “Operator terner bersyarat”

Pertanyaan yang mirip dengan “Operator terner bersyarat”

Lebih banyak jawaban terkait untuk “Operator terner bersyarat” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya