The ternary operator is a conditional operator that evaluates a condition and returns one of two values based on whether the condition is true or false. It is a shorthand for if-else statements and is supported in many programming languages, though syntax varies.

JavaScript

In JavaScript, the ternary operator uses the syntax:

condition ? exprIfTrue : exprIfFalse
  • Example: const age = 26; const beverage = age >= 21 ? "Beer" : "Juice";

  • It is right-associative, allowing chaining like a ? b : c ? d : e, equivalent to a ? b : (c ? d : e).

  • It is widely used for concise conditional assignments and is available across all modern browsers since July 2015.

C, C++, Java, and similar languages

These use the ?: syntax:

variable = condition ? expression1 : expression2;
  • Only the selected expression is evaluated (short-circuit evaluation).

  • Example: int max = (a > b) ? a : b;

  • The operator has low precedence, so parentheses are often needed.

Python

Python uses a different, more readable syntax:

value_if_true if condition else value_if_false
  • Example: status = "Adult" if age >= 18 else "Minor"

  • It is officially called a conditional expression.

  • Unlike other languages, it does not use ? : syntax.

  • Important: The tuple-based workaround (if_false, if_true)[condition] is discouraged because both values are evaluated, which can cause errors (e.g., 1/0).

Key Benefits

  • Conciseness: Reduces code length for simple decisions.

  • Readability: Improves clarity for straightforward conditions.

  • Variable Initialization: Allows assignment during declaration, even for const variables.

Best Practices

  • Use for simple, clear conditions.

  • Avoid nesting multiple ternary operators to maintain readability.

  • Avoid side effects (e.g., function calls, assignments) in expressions, as they may lead to unpredictable behavior.

Note: While the ternary operator is powerful, complex logic should use if-else for better maintainability.

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Conditional_operator
Conditional (ternary) operator - JavaScript | MDN
The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark (?), then an expression to execute if the condition is truthy followed by a colon (:), and finally the expression to execute if the condition is falsy.
ternary operator "x ? y : z" in many programming languages, whose value is y if x evaluates to true and z otherwise
In computer programming, the ternary conditional operator is a ternary operator that evaluates to one of two values based on a Boolean expression. The operator is also known as conditional operator, ternary … Wikipedia
🌐
Wikipedia
en.wikipedia.org › wiki › Ternary_conditional_operator
Ternary conditional operator - Wikipedia
January 27, 2026 - In computer programming, the ternary conditional operator is a ternary operator that evaluates to one of two values based on a Boolean expression. The operator is also known as conditional operator, ternary if, immediate if, or inline if (iif). Although many ternary operators are theoretically ...
Discussions

What are pros/cons of ternary conditional operators? - Programming Language Design and Implementation Stack Exchange
There are some languages that support ternary operator. There are Python, C, JavaScript, PHP. In languages like C and JavaScript, the syntax is like this: condition ? do_true : do_false Python has More on langdev.stackexchange.com
🌐 langdev.stackexchange.com
June 23, 2023
c# - How does the ternary operator work? - Stack Overflow
Please demonstrate how the ternary operator works with a regular if/else block. Example: Boolean isValueBig = value > 100 ? true : false; Exact Duplicate: How do I use the ternary operator? More on stackoverflow.com
🌐 stackoverflow.com
Ternary Operator
I generally use it for a conditional assignment, where a variable will have one of two values based on some condition. I probably wouldn't chain them, though. Also, I like to enclose the condition in parentheses, even though it isn't required. It helps me to parse it as a condition instead of the result of the expression. This is just how I use the operator, though, and my own practices shouldn't be taken as gospel. More on reddit.com
🌐 r/C_Programming
60
49
May 14, 2023
Ternary conditional operator : golang
Why Golang doesn't have ?: operator? It's very useful. But instead of this: `fmt.Println(a == 0 ? "Zero" : "Not zero")` I have to write... More on old.reddit.com
🌐 r/golang
🌐
W3Schools
w3schools.com › c › c_conditions_short_hand.php
C Short Hand If ... Else (Ternary Operator)
The ternary operator returns a value based on a condition: if the condition is true, it returns the first value; otherwise, it returns the second value.
🌐
Programiz
programiz.com › java-programming › ternary-operator
Java Ternary Operator (With Example)
A ternary operator evaluates the test condition and executes a block of code based on the result of the condition.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › csharp › language-reference › operators › conditional-operator
?: operator - the ternary conditional operator - C# reference | Microsoft Learn
January 24, 2026 - The conditional operator ?:, also known as the ternary conditional operator, evaluates a Boolean expression and returns the result of one of the two expressions, depending on whether the Boolean expression evaluates to true or false, as the ...
🌐
Igmguru
igmguru.com › blog › ternary-operator-in-python
Ternary Operator in Python: Complete Tutorial for Beginners
3 days ago - The ternary operator in Python is a powerful and efficient way to handle simple conditional assignments. It simplifies code and improves clarity in straightforward scenarios. It is commonly used in functions, lambda expressions and list ...
Find elsewhere
🌐
DEV Community
dev.to › valentinaperic › writing-if-statements-with-the-ternary-operator-37j
Writing If Statements with the Ternary Operator - DEV Community
August 25, 2020 - The conditional (ternary) operator ... a condition followed by a question mark (?), then an expression to execute if the condition is truthy followed by a colon (:), and finally the expression to execute if the condition is falsy...
🌐
Codecademy
codecademy.com › docs › operators › ternary operator
C | Operators | Ternary operator | Codecademy
February 5, 2025 - The ternary operator in C, also known as the conditional operator (?:), provides a shorthand way to perform conditional assignments or expressions.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › conditional-or-ternary-operator-in-c
Conditional or Ternary Operator (?:) in C - GeeksforGeeks
July 12, 2025 - The conditional operator in C is ... the shortest way possible. It is also known as the ternary operator in C as it operates on three operands....
🌐
Brohn Homes
brohnhomes.com
Find Your New Austin Or Central Texas Home Today
Brohn Homes builds quality new homes in Austin, San Antonio, and Houston—by Texans, for Texans. More home for your money, built with love.
🌐
W3Schools
w3schools.com › js › js_if_ternary.asp
W3Schools.com
The conditional operator is a shorthand for writing conditional if...else statements. It is called a ternary operator because it takes three operands.
🌐
Microsoft Support
support.microsoft.com › en-us › office › if-function-69aed7c9-4e8a-4755-a9bc-aa8bbff73be2
IF function - Microsoft Support
In this example, the formula in cell D2 says: IF(C2 = 1, then return Yes, otherwise return No)As you see, the IF function can be used to evaluate both text and values. It can also be used to evaluate errors. You are not limited to only checking if one thing is equal to another and returning a single result, you can also use mathematical operators and perform additional calculations depending on your criteria.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-ternary-operator
Java Ternary Operator - GeeksforGeeks
The ternary operator is a compact alternative to the if-else statement. It evaluates a condition and returns one of two values depending on whether the condition is true or false.
Published   December 20, 2025
🌐
Igmguru
igmguru.com › blog
Blog
December 3, 2025 - DevOps is basically a blend of two words: ‘Development’ and ‘Operations’. When combined, they become a modern approach where software developers and software operations teams work together throughout the whole software lifecycle. Get into the..
🌐
Harness Developer Hub
developer.harness.io › continuous delivery & gitops › knowledge base article › use ternary operators with triggers
Use ternary operators with triggers | Harness Developer Hub
October 10, 2025 - The ternary operator, also known as the conditional operator, is a shorthand notation for expressing conditional statements in various programming languages.
🌐
Google
google.github.io › styleguide › pyguide.html
styleguide | Style guides for Google-originated open-source projects
For common operations like multiplication, use the functions from the operator module instead of lambda functions. For example, prefer operator.mul to lambda x, y: x * y. Okay for simple cases. Conditional expressions (sometimes called a “ternary operator”) are mechanisms that provide a ...
🌐
Python Tips
book.pythontips.com › en › latest › ternary_operators.html
6. Ternary Operators — Python Tips 0.1 documentation
Ternary operators are more commonly known as conditional expressions in Python. These operators evaluate something based on a condition being true or not.