4. Write only the syntax of ternary(conditional) operator in javascript.
Answers
Solution :-
A ternary operator is the one which uses three operands to work.
Symbol - ( ? : )
Syntax :-
1. If the condition is true, statement 1 will be executed.
2. If the condition is false, statement 2 will be executed.
Example to explain it's functioning -
First Example
Here,
Condition is - 10 < 20
First Statement is - "Yes"
Second Statement is - "No"
Output - in the attachment.
The condition is asking that 20 is greater than 10. Obviously, we all know that 20 is greater 10. So, the condition is true. Therefore, "Yes" will be printed.
Second Example
Condition is - 10 > 20
First Statement is - "Yes"
Second Statement is - "No"
Output - in the attachment.
Here, the condition is asking that 10 is greater than 20.
So, the condition becomes false as 20 is greater than 10. Therefore, second statement "No" will be printed.