Learn JavaScript

JavaScript Conditional Operators

JavaScript conditional operator (?) is the only operator in JavaScript programming that requires three operands to perform any action. Because of such quality the conditional operator is also known as a ternary operator. The basic Syntax of the JavaScript conditional operator is as:

condition ? Expression-1 : Expression-2

Condition: It represents an expression, whose truth value is used as a condition.

Expression-1: It represents an expression, that will be executed if the truth value of the “condition” is true.

Expression-2: It represents an expression, that will be executed if the truth value of the “condition” is false.

Note: null, NaN, 0, empty string, and undefined are also treated as the false truth value.


Example

<html>
<body>
<script>
function jco(){
var age = 24;
var drink = (age >= 18) ? "You are 18+ so you can enjoy any drink" : "You are below 18 so you are aloowed to drink only soft drinks";
document.write(drink);
}
jco();
</script>
</body>
</html>

Output

You are 18+ so you can enjoy any drink


Example

<html>
<body>
<script>
function jco(){
var age = 17;
var drink = (age >= 18) ? "You are 18+ so you can enjoy any drink" : "You are below 18 so you are aloowed to drink only soft drinks";
document.write(drink);
}
jco();
</script>
</body>
</html>

Output

You are below 18 so you are allowed to drink only soft drinks


Comparison of Different Datatypes

Comparison of different datatypes, may results some unexpected results. On comparing a string with a number, JavaScript first convert the string into a number, then it compare this number with other number. An empty string is converted to 0, while a non numeric string converts into NaN that always return false truth value.

<html>
<body>
<h2>Understanding the Comparison of Different Datatypes</h2>
<script>
function jco(){
var age = 17;
// Comparison with a number
var a = (age < 18) ? "You are Adult Now" : "You are Still a Teenager";
document.write(a);
document.write("<br>");
// Comparison with a string
var b = (age > "18") ? "You are Adult Now" : "You are Still a Teenager";
// JavaScript convert the string 18 to number 18 then compares
document.write(b);
}
jco();
</script>
</body>
</html>

Output

Understanding the Comparison of Different Datatypes

You are Adult Now
You are Still a Teenager


Audience

This JavaScript tutorial is designed to help the beginners as well as the professional developers. For better understanding the language of the tutorial is kept simple and easy to understand.

Prerequisites

This tutorial demands to have the basic knowledge of the programming languages such as c or c++, HTML for the better understanding of the JavaScript programming language. The other basic requirement to learn JavaScript through this tutorial is the basic knowledge of computer and information technology so you can easily acquire a better understanding JavaScript programming.

Community Helping

In case you find any kind of error like typing error, spelling mistakes, incorrect grammar, incorrect information or any other mistakes. Please feel free to write to us at: akyadav@etutororg.com.