JavaScript Conditional Operator
Notes:
JavaScript Conditional Operator:
Conditional Operator (?:):
is the only ternary operator in JavaScript. It accepts 3 operands.
(operand1) ? operand2 : operand3;
Where:
operand1: should be a condition
operand2: value1
operand3: value2
Ex:
var num=10;
var result = ( (num%2)==0) ? “even” : “odd”;
document.write(num," is ", result, " number");
Note: Conditional operator is kind of short hand notation for if else statement in JavaScript.
Ex:
var num=10;
var result;
if((num%2)==0)
{
result=”even”;
}
else
{
result = “odd”;
}
document.write(num," is ", result, " number");
Interview Questions: