Choose between branches with if / else if / else, and pick a value with condition ? a : b.
JavaScript · beginner · 5 min · 8 steps · free
function grade(score) {
if (score >= 90) {
return 'A';
} else if (score >= 75) {
return 'B';
}
return 'C';
}
const result = grade(82);
const label = result === 'A' ? 'excellent' : 'good';
console.log(result, label);grade already exists. This is called hoisting.grade with score = 82.score >= 75 is true, so the if-block runs.return ends the function at once: the last line, return "C", never runs.grade() has finished, so its variables are gone. The value it returned, "B", is stored in result.condition ? a : b picks a value: result is not "A", so label = "good".B good.Answer: "A"
Answer: 1
← Strings and Template Literals · Loops: for, while and for…of →