
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
What is a ternary operator (?:) in JavaScript?
The conditional operator or ternary operator first evaluates an expression for a true or false value and then executes one of the two given statements depending upon the result of the evaluation.
S.No | Operator & Description |
---|---|
1 | ? : (Conditional ) If Condition is true? Then value X: Otherwise value Y |
Example
You can try to run the following code to understand how Ternary Operator works in JavaScript
<html> <body> <script> var a = 10; var b = 20; var linebreak = "<br />"; document.write ("((a > b) ? 100 : 200) => "); result = (a > b) ? 100 : 200; document.write(result); document.write(linebreak); document.write ("((a < b) ? 100 : 200) => "); result = (a < b) ? 100 : 200; document.write(result); document.write(linebreak); </script> </body> </html>
- Related Articles
- What is a Ternary operator/conditional operator in C#?
- Changing ternary operator into non-ternary - JavaScript?
- What is ternary operator in C#?
- What is ternary operator (? X : Y) in C++?
- Ternary Operator in Java
- Ternary Operator in Python?
- Ternary Operator in C#
- Conditional ternary operator ( ?: ) in C++
- Ternary Operator in Dart Programming
- C/C++ Ternary Operator
- Java Ternary Operator Examples
- How Ternary operator in PowerShell Works?
- Does Python have a ternary conditional operator?
- Is there an equivalent of C’s “?:” ternary operator in Python?
- How to overload python ternary operator?

Advertisements