What does The question Mark means? | Sololearn: Learn to code for FREE!


What does The question Mark means?

var age = 42; var isAdult = (age < 18) ? "Too young": "Old enough"; document.write(isAdult);

1/27/2017 7:40:08 PM

Andrei Iliescu

7 Answers

New Answer


If age < 42 then isAdult = "too young" else isAdult = "old enough"


(condition) ? (result if true) : ( result if false) so your example is equivalent to: if (age < 18) isAdult ="Too young"; else isAdult = "Old enough";


? tells the compiler that the "Too young":"Old enough" are used as the answer of the boolean operation before


this one is called conditional operator (or sometimes just ternary operator) let me explain it this way the value on the left of the question mark picks which of the other two values will come out. when it is true the middle one value is chosen, and when it is false, the value on the right comes out for example console.log(true ? 1 : 2); in this case 1 comes out... console.log(false ? 1 : 2); in this case 2 comes out ...




That is called `Ternary Operator`. It's like the; If else condition that was written on a single line instead.