- 4 years ago
- Zaid Bin Khalid
- 5310 Views
-
6
In this session, you will learn how to write the decision-making code by conditional statements IF and ELSE in JavaScript with the help of an example.
JavaScript Conditional Statements
Like many other programming language JavaScript also allow you to write code which can perform many actions. Those actions based on the result of a logical or comparative test condition at run time.
You can also create conditions like test conditions in the form of expression that evaluates to true or false, and based on these results. You can perform specific actions.
JavaScript Conditions
These are the several conditions of JavaScript. These conditions will use to make different decisions.
- if statement
- if…else statement
- if…else if….else statement
- switch…case statement
The if Statement
The “if statement” is used to complete the block of code. This only is done if the condition, specified will evaluates to true.
If statement Code
The basic and simple JavaScript if statement will show as.
if(condition) {
// code to be executed
}
Output- If Statement Code
The example result output will show as “Have a nice weekend!” if the current day is Friday.
var now = new Date();
var dayOfWeek = now.getDay(); // Sunday - Saturday : 0 - 6
if(dayOfWeek == 5){
alert("Have a nice weekend!");
}
The if…else Statement
You can increase the capabilities of your JavaScript program decision by adding a choice through an else statement to if statement.
If the executed condition is evaluated to true, the if..Else statement will execute the one block code.
If…Else statement Code
The basic and simple JavaScript if…else statement will show as:
if(condition) {
// code to be executed if the condition is true
} else {
// code to be executed if the condition is false
}
The JavaScript code in the example will show the output result of “Have a nice weekend!” if it is a Friday. Else your resulting output will be “Have a nice day!”.
var now = new Date();
var dayOfWeek = now.getDay(); // Sunday - Saturday : 0 - 6
if(dayOfWeek == 5) {
alert("Have a nice weekend!");
} else {
alert("Have a nice day!");
}
The if…else if…else Statement
The if…else if…else is a unique statement which is used to combine if…else statements multiple time.
if(condition1) {
// Code to be executed if condition1 is true
} else if(condition2) {
// Code to be executed if the condition1 is false and condition2 is true
} else {
// Code to be executed if both condition1 and condition2 are false
}
Output – If…Else statement Code
The example result output will show as “Have a nice weekend!” If the current day is Friday. And “Have a nice Sunday!” if the present day is Sunday.
Else, it will give you the following output as “Have a nice day!”.
var now = new Date();
var dayOfWeek = now.getDay(); // Sunday - Saturday : 0 - 6
if(dayOfWeek == 5) {
alert("Have a nice weekend!");
} else if(dayOfWeek == 0) {
alert("Have a nice Sunday!");
} else {
alert("Have a nice day!");
}
The Ternary Operator
Ternary operators allow a shorthand way of writing if…else statements. The ternary operator is represented by a question mark symbol (?) and three basic operations. Check the condition, which is true and false.
The basic syntax of the ternary operator as shown.
var result = (condition) ? value1 : value2
If the value1 condition is evaluated to true, it will be returned. The value2 will return as follow. To understand how this operator works, consider the examples as shown.
var userType;
var age = 21;
if(age < 18) {
userType = 'Child';
} else {
userType = 'Adult';
}
alert(userType); // Displays Adult
By using the ternary operator, the same code can be written more compact way.
var age = 21;
var userType = age < 18 ? 'Child' : 'Adult';
alert(userType); // Displays Adult
Look into the above example you can see, the specified condition evaluated to false the value on the right side of the (:) colon is returned, which is the ‘Adult’ String.
- 4 years ago
- Zaid Bin Khalid
- 5310 Views
-
6