A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/javascript-ternary-operator/ below:

JavaScript Ternary Operator - GeeksforGeeks

JavaScript Ternary Operator

Last Updated : 28 Jul, 2025

The Ternary Operator in JavaScript is a conditional operator that evaluates a condition and returns one of two values based on whether the condition is true or false. It simplifies decision-making in code, making it more concise and readable.

Syntax

condition ? trueExpression : falseExpression

Now let's understand this with the help of example:

JavaScript
// JavaScript to illustrate Conditional operator
let PMarks = 50;
let res = (PMarks > 39) ? "Pass" : "Fail";

console.log(res);

Output

Pass

In this example

Nested Ternary Operators

The ternary operator can be nested, allowing you to perform multiple conditional checks in a single line of code. This technique is useful for replacing more complex if...else if...else statements or switch statements, keeping the code compact and readable.

JavaScript
let day = 3;
let greeting = (day === 1) ? 'Start of the week' :
               (day === 2) ? 'Second day' :
               (day === 3) ? 'Midweek' :
               (day === 4) ? 'Almost weekend' :
               'Weekend';

console.log(greeting); 

Output

Midweek

In this Example

Ternary Operator in Functions

The ternary operator can also be used inside functions to simplify conditional logic. It helps make functions more concise by replacing if...else statements with a single-line expression.

JavaScript
function checkAge(age) {
  return (age >= 18) ? 'Adult' : 'Minor';
}

console.log(checkAge(20));  
console.log(checkAge(15)); 

Output

Adult
Minor

In this example

Using the Ternary Operator with Function Calls

You can use the ternary operator to decide which function to call or what arguments to pass when calling a function. This makes your code shorter and easier to read by replacing long if...else statements.

JavaScript
function sayHello(name) {
  console.log(`Hello, ${name}!`);
}

function sayGoodbye(name) {
  console.log(`Goodbye, ${name}!`);
}

let isLeaving = true;
let name = 'Geeks';

isLeaving ? sayGoodbye(name) : sayHello(name);  

Output

Goodbye, Geeks!

In this example

A Comparison of the Ternary Operator and the if...else Statement

Understanding the difference between the ternary operator and the traditional if...else statements is key in determining which to use for concise and readable code.

Using if...else Statement

The if...else statement is a basic control structure that allows you to perform different actions based on a condition. It is typically used when you need to execute more complex or multiple statements depending on a condition.

JavaScript
let hour = 15;
let message;

if (hour < 12) {
  message = 'Good morning';
} else {
  message = 'Good afternoon';
}

console.log(message); 

Output

Good afternoon

In this example

Using the Ternary Operator

The ternary operator is a shorthand way of writing an if...else statement. It is most useful when you want to assign values based on a simple condition, making the code more compact.

JavaScript
let hour = 15;
let message = (hour < 12) ? 'Good morning' : 'Good afternoon';

console.log(message); 

Output

Good afternoon

In this example



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4