what is the meaning of question mark in javascript

Crafts from polymer clay with their own hands. A large selection of tips and examples of products from polymer clay https://clay-crafts.com/

In JavaScript, the question mark (?) has several meanings, including its usage as the ternary conditional operator, which provides a shorthand way to write conditional expressions.

Ternary Conditional Operator

The question mark is used in a conditional expression to provide an if-else alternative in a concise manner.

Alles über Träume und Träume. Interpretation und Bedeutung der Träume https://traumauslegung.com/

Usage and Example

Here’s an example illustrating the use of the question mark as the ternary conditional operator:

    const age = 18;
    const isAdult = (age >= 18) ? "Yes" : "No";
    console.log(isAdult); // Output: "Yes"

Nullish Coalescing Operator

In recent JavaScript versions, the question mark can also be used as the nullish coalescing operator, which returns the right-hand operand when the left-hand operand is null or undefined.


The question mark in JavaScript is versatile, serving as the ternary conditional operator for concise conditionals and, in modern JavaScript, as the nullish coalescing operator for handling null or undefined values.

Educational Encyclopedia