Ternary Operator: The “Question mark” or “conditional” operator in JavaScript is known as ternary operator that has three operands. It is used to simplify the if/else operator.

Examples:

Input: let result = (10 > 0) ? true : false;

Output: true

Input: let message = (20 > 15) ? "Yes" : "No";
Output: Yes

Syntax:

condition ? value if true : value if false
  • condition: Expression to be evaluated which returns a boolean value.
  • value if true: Value to be executed if the condition results in a true state.
  • value if false: Value to be executed if the condition results in a false state.

Characteristics of Ternary Operator:

The expression consists of three operands: the condition, value if true, and value if false.

  • The evaluation of the condition should result in either true/false or a boolean value.
  • The true value lies between “?” & “:” and is executed if the condition returns true. Similarly, the false value lies after “:” and is executed if the condition returns false.

Example 1: Below is an example of the Ternary Operator.

JavaScript

function test() {
    // JavaScript to illustrate
    // Conditional operator
    let PMarks = 40
    let result = (PMarks > 39) ?
        "Pass" : "Fail";
    console.log(result);
}
test();

Output:

Pass

Example 2: Below is an example of the Ternary Operator.

JavaScript

function test() {
    // JavaScript to illustrate
    // Conditional operator
    let age = 60
    let result = (age > 59) ?
        "Senior Citizen" : "Not a Senior Citizen";
    console.log(result);
}
test();

Output:

Senior Citizen

Example 3: An example of multiple conditional operators.

JavaScript

function test() {
    // JavaScript to illustrate
    // multiple Conditional operators
    let marks = 95;
    let result = (marks < 40) ? "Fail" :
        (marks < 60) ? "Average" :
            (marks < 80) ? "Good" : "Excellent";
    console.log(result);
}
test();

Output:

Excellent
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, codeconfig.in Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Explore More

How to Center a Popup Window on Screen

JavaScript window.open() method is used to open a popup window. This popup window will be placed in the center of the screen. This example creates the pop-up window without placing it into

How to hide URL in the popup window opened using window.open

If you are using below code to open a popup window in your web page, then address must be appearing in you pop up window and f you want to

window.showModalDialog is deprecated in Edge and Chrome

If you have a website being compatible with Edge/Chrome and in past it was only compatible with IE 11 and if you are using window.showModalDailog in your JavaScript code, then