Conditional Operator
The conditional operator has the following form:
variable = boolean_expression ? true_value : false_value;
It means
if boolean_expression is true
assign true_value to variable
else if boolean_expression is false
assign false_value to variable
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
var num1 = 3;
var num2 = 4;
var max = (num1 > num2) ? num1 : num2;
document.writeln(max);//4
</script>
</head>
<body>
</body>
</html>
Home
JavaScript Book
Language Basics
JavaScript Book
Language Basics
Operators:
- JavaScript Operators
- Increment/Decrement Operators
- Increment/Decrement Operators for String, Boolean, Floating-point and Object
- Unary Plus and Minus
- Bitwise Not operator
- Bitwise AND
- Bitwise OR
- Bitwise XOR
- Left Shift
- Signed Right Shift
- Unsigned Right Shift
- Logical NOT
- Logical AND
- Logical OR
- Multiply
- Divide
- Modulus
- Add
- Subtract
- Relational Operators
- Equal and Not Equal
- Identically Equal and Not Identically Equal
- Conditional Operator
- Assignment Operators
- Comma Operator