Skip to content

? in JavaScript

While reading JavaScript codes, you may find "?" operator in a high frequency.

(condition) ? truePart : falsePart; is rough equivalent of:

if (condition) {
    truePart;
}
else {
    falsePart;
}

The same shorthand method can also be used to set variables. For instance: myNewVariable = (condition) ? trueValue : falseValue; is equivalent to:

if (condition) {
    myNewVariable = trueValue;
}
else {
    myNewVariable = falseValue;
}

天上的神明与星辰,人间的艺术和真纯,我们所敬畏和热爱的,莫过于此。