Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

JavaScript 006 – If and if…else conditions

1. JavaScript if Condition – Simple

Just like other programming language, JavaScript (Scripting language) also provides decision-making structures using the if and else construct. The if statement comes with the condition which will evaluate to true or false. Now, have a look at the below code:

Javascript: Simple If Condittion
Javascript: Simple If Condittion

1) The prompt method call will ask the user to input a number. Whatever user puts in the box is returned and stored in the variable strTheNumber. To covert the string to a number (Decimal format, base 10), we use the method parseInt.

2) Next, we form the condition using the if statement. The equality boolean operator == checks the variable Number is holding the value 0 and based on the test, it will return boolean true or false.

3) JavaScript enters this code block when the condition evaluates to true. Here, in the code block, we have only one statement which prints a message to the browser window. It is possible to have multiple statement inside this code block.

The output of running the script at this stage is shown below:

Simple If Condittion_Output
Simple If Condittion_Output

2. JavaScript If with Else

The if statement evaluates a condition and when the condition outcome is true, the control flow enters the if block. Sometimes, we need to execute another piece code when the condition’s outcome is false. Now look at the below code:

JavaScript If Else
JavaScript If Else

1) The if is accompanied with a condition Number < 0. This condition when evaluates to true enters the code block enclosed within the pair of Curley braces { & }. Here, in the code block, we print the number is Negative.

2) Instead of putting one more if condition for the Positive number, we used the else block. When the condition at evaluates to false, we land up here executing the document print which says the number is positive.

Now the output is below:

JavaScript If Else - Outtput
JavaScript If Else – Outtput

3. Combining Multiple Conditions

Sometimes, we may need to combine multiple conditions while doing the decision making. With the below logical operators, one can combine multiple conditions:

  1. Logical AND (&&) – This operator makes sure all combined conditions evaluate to true to get a net result of true.
  2. Logical OR (||) – This operator will be useful if you want the net outcome as true even a single condition evaluates to true.
  3. Logical Not (!) – Useful to invert the conditional outcome.

Have a look at the below example:

JavaScript: Combine Multiple Condition
JavaScript: Combine Multiple Condition

1) Here we check the number should be greater than 9. This is the first condition.

2) In this condition, we check the number should be less than 26.

The Logical AND (&&) combines both the above conditions. Outcome of the if condition will be true only when both the condition evaluates to true. Above combined condition will produce the net result of the flow entering the if block when the number is in the Range of Ten to Twenty-five.

Here is the output:

Combine Multiple Condition-Output
Combine Multiple Condition-Output

4. Code Reference

Categories: JavaScript

Tags: , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.