Ternary Operators are basically replacement for if-else condition.
Below is the example for java:
if(condition == true)
{
System.out.println("Condition is true");
}
else
{
System.out.println("Condition is false");
}
The above example can be replaced by a ternary operator as :
(condition == true)?System.out.println("Condition is true"):System.out.println("Condition is false");
The syntax remains almost same for javascript :
if(condition == true)
{
alert("Condition is true");
}
else
{
alert("Condition is false");
}
(condition == true)?alert("Condition is true"):alert("Condition is false");
Ternary Operators reduces the readability of the code little bit but they give a better performance. If you are using if-else condition and there are millions of records to be processed then go for ternary operator.
Below is the example for java:
if(condition == true)
{
System.out.println("Condition is true");
}
else
{
System.out.println("Condition is false");
}
The above example can be replaced by a ternary operator as :
(condition == true)?System.out.println("Condition is true"):System.out.println("Condition is false");
The syntax remains almost same for javascript :
if(condition == true)
{
alert("Condition is true");
}
else
{
alert("Condition is false");
}
(condition == true)?alert("Condition is true"):alert("Condition is false");
Ternary Operators reduces the readability of the code little bit but they give a better performance. If you are using if-else condition and there are millions of records to be processed then go for ternary operator.
Comments
Post a Comment
.