This is a one-line shorthand for an if-else statement. It's called the conditional operator.1

Here is an example of code that could be shortened with the conditional operator:

var userType;
if (userIsYoungerThan18) {
  userType = "Minor";
} else {
  userType = "Adult";
}

if (userIsYoungerThan21) {
  serveDrink("Grape Juice");
} else {
  serveDrink("Wine");
}

This can be shortened with the ?: like so:

var userType = userIsYoungerThan18 ? "Minor" : "Adult";

serveDrink(userIsYoungerThan21 ? "Grape Juice" : "Wine");

Like all expressions, the conditional operator can also be used as a standalone statement with side-effects, though this is unusual outside of minification:

userIsYoungerThan21 ? serveGrapeJuice() : serveWine();

They can even be chained:

serveDrink(userIsYoungerThan4 ? 'Milk' : userIsYoungerThan21 ? 'Grape Juice' : 'Wine');

Be careful, though, or you will end up with convoluted code like this:

var k = a ? (b ? (c ? d : e) : (d ? e : f)) : f ? (g ? h : i) : j;

1 Often called "the ternary operator," but in fact it's just a ternary operator [an operator accepting three operands]. It's the only one JavaScript currently has, though.

Answer from Peter Olson on Stack Overflow
🌐
Technical Mickey
technicalmickey.com › tutorials › 20-javascript-questions-on-conditional-statements-ternary-operator
20 JavaScript Questions on Conditional Statements, Logical & Ternary Operators
June 27, 2025 - Practice and improve your JavaScript skills with these 20 questions focused on conditional statements, logical operators (&&, ||, !), and the ternary operator. Perfect for beginners and interview preparation.
Top answer
1 of 16
771

This is a one-line shorthand for an if-else statement. It's called the conditional operator.1

Here is an example of code that could be shortened with the conditional operator:

var userType;
if (userIsYoungerThan18) {
  userType = "Minor";
} else {
  userType = "Adult";
}

if (userIsYoungerThan21) {
  serveDrink("Grape Juice");
} else {
  serveDrink("Wine");
}

This can be shortened with the ?: like so:

var userType = userIsYoungerThan18 ? "Minor" : "Adult";

serveDrink(userIsYoungerThan21 ? "Grape Juice" : "Wine");

Like all expressions, the conditional operator can also be used as a standalone statement with side-effects, though this is unusual outside of minification:

userIsYoungerThan21 ? serveGrapeJuice() : serveWine();

They can even be chained:

serveDrink(userIsYoungerThan4 ? 'Milk' : userIsYoungerThan21 ? 'Grape Juice' : 'Wine');

Be careful, though, or you will end up with convoluted code like this:

var k = a ? (b ? (c ? d : e) : (d ? e : f)) : f ? (g ? h : i) : j;

1 Often called "the ternary operator," but in fact it's just a ternary operator [an operator accepting three operands]. It's the only one JavaScript currently has, though.

2 of 16
183

I want to add some to the given answers.

In case you encounter (or want to use) a ternary in a situation like 'display a variable if it's set, else...', you can make it even shorter, without a ternary.


Instead of:

var welcomeMessage  = 'Hello ' + (username ? username : 'guest');

You can use:

var welcomeMessage  = 'Hello ' + (username || 'guest');

This is Javascripts equivallent of PHP's shorthand ternary operator ?:

Or even:

var welcomeMessage  = 'Hello ' + (username || something || maybethis || 'guest');

It evaluates the variable, and if it's false or unset, it goes on to the next.

🌐
Hellojavascript
hellojavascript.info › general javascript questions › javascript fundamentals › logical operators
Logical Operators | JavaScript Frontend Interview Answers
In this example, the logical AND operator (&&) is used to check if both x is greater than 0 and y is less than 20. If both conditions are true, it will print "Both conditions are true!" to the console. Otherwise, it will print "At least one condition is false." ... Interview Response: Operands evaluate from left to right, then it converts each operand to a boolean value, and if the result is false, the program terminates and returns the operand's original value.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-course-conditional-operator-in-javascript
JavaScript Course Conditional Operator in JavaScript - GeeksforGeeks
July 11, 2025 - The above code is a very simple demonstration of "if" the conditional operator and if we change the value of age to something other than '20' then nothing prints. The 'if(..)' statement evaluates the expression inside its parentheses and then converts it into a boolean value. If that boolean value is a 'false' then the output will not be printed. if(0){ console.log('hey'); // Will not be printed } ... The else clause executes when the condition inside the if parenthesis fails.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Conditional_operator
Conditional (ternary) operator - JavaScript | MDN
This operator is frequently used as an alternative to an if...else statement. function getFee(isMember) { return isMember ? "$2.00" : "$10.00"; } console.log(getFee(true)); // Expected output: "$2.00" console.log(getFee(false)); // Expected output: "$10.00" console.log(getFee(null)); // Expected output: "$10.00" ... An expression whose value is used as a condition.
🌐
TutorialsPoint
tutorialspoint.com › javascript › javascript_conditional_operators.htm
JavaScript - Conditional Operators
The JavaScript conditional (ternary) operator is only operator that takes three operands a condition followed by a question mark (?), then the first expression to be executed if the condition is truthy followed by a colon (:), and finally the second expression to be executed if the condition ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-ternary-operator
JavaScript Ternary Operator - GeeksforGeeks
// JavaScript to illustrate Conditional operator let PMarks = 50; let res = (PMarks > 39) ?
Published   July 28, 2025
Find elsewhere
🌐
The Web Dev
thewebdev.info › home › javascript interview questions — operators
JavaScript Interview Questions — Operators - The Web Dev
August 14, 2020 - The spread operator is useful for merging objects and arrays or making copies of them. It can also be used to pass an array into a function as arguments. ... The JavaScript conditional operator is a basic building block of JavaScript programs.
🌐
W3Schools
w3schools.com › js › js_if_ternary.asp
JavaScript Conditional Ternary Operator
JS Examples JS HTML DOM JS HTML ... Plan JS Interview Prep JS Bootcamp JS Certificate JS Reference ... The conditional operator is a shorthand for writing conditional if...else statements. It is called a ternary operator because it takes three operands. ... The conditional (ternary) operator is the only JavaScript operator that ...
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › javascript fundamentals
Conditional branching: if, '?'
To do that, we can use the if statement and the conditional operator ?, that’s also called a “question mark” operator.
🌐
GreatFrontEnd
greatfrontend.com › questions › quiz › what-is-the-ternary-operator-and-how-is-it-used
What is the ternary operator and how is it used? | Quiz Interview Questions with Solutions
September 5, 2021 - The ternary operator is a shorthand for an if-else statement in JavaScript. It takes three operands: a condition, a result for true, and a result for false. The syntax is condition ? expr1 : expr2. For example, let result = (a > b) ?
🌐
CodeKit
codekit.in › javascript-interview-questions
Best JavaScript Interview Questions And Answers by CodeKit.
Logical operators in JavaScript are used to combine or manipulate Boolean values or expressions. They are frequently used in conditional statements to control the flow of a program. JavaScript has three main logical operators: && (logical AND), || (logical OR), and !
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › conditional-statements-in-javascript
JavaScript - Conditional Statements - GeeksforGeeks
Avoid Deep Nesting: Use logical operators (&&, ||) or early returns to flatten your code structure. Include Default Cases: Use else or default to handle unexpected conditions. Comment Complex Conditions: Explain the logic for future reference and easier maintenance. Test All Possible Paths: Make sure every branch of your condition executes as expected during testing. Conditional statements in JavaScript help your code make decisions and run different actions based on conditions.
Published   June 21, 2025
🌐
InterviewPrep
interviewprep.org › conditional-statements-interview-questions
Top 25 Conditional Statements Interview Questions and Answers - InterviewPrep
April 30, 2025 - A ternary operator is more appropriate when dealing with simple, single condition checks that return a value. For instance, in JavaScript, you might want to assign a variable based on a boolean condition.
🌐
GitHub
github.com › sudheerj › javascript-interview-questions
GitHub - sudheerj/javascript-interview-questions: List of 1000 JavaScript Interview Questions
Ternary Operators: It includes conditional(: ?) Operator
Starred by 26.7K users
Forked by 7.5K users
Languages   JavaScript
🌐
W3Schools
w3schools.com › js › js_comparisons.asp
JavaScript Comparison Operators
JS Examples JS HTML DOM JS HTML Input JS HTML Objects JS HTML Events JS Browser JS Editor JS Exercises JS Quiz JS Website JS Syllabus JS Study Plan JS Interview Prep JS Bootcamp JS Certificate JS Reference ... Comparison operators are used to compare two values. Comparison operators always return true or false. Given that x = 5, the table below explains the comparison operators: Comparison operators can be used in conditional statements to compare values and take action depending on the result:
🌐
Codefinity
codefinity.com › blog › Top-50-Must-Know-JavaScript-Interview-Questions
Top 50 Must Know JavaScript Interview Questions
Conditional statements in JavaScript include if, else if, and else, which allow you to execute different blocks of code based on different conditions. 16. What is the typeof operator in JavaScript?
🌐
InterviewBit
interviewbit.com › javascript-interview-questions
JavaScript Interview Questions and Answers (2025) - InterviewBit
The difference between both the operators is that “==” is used to compare values whereas, “ === “ is used to compare both values and types. ... var x = 2; var y = "2"; (x == y) // Returns true since the value of both x and y is the same ...
Published   December 20, 2024