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:

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:
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:

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:
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:
- Logical AND (&&) – This operator makes sure all combined conditions evaluate to true to get a net result of true.
- Logical OR (||) – This operator will be useful if you want the net outcome as true even a single condition evaluates to true.
- Logical Not (!) – Useful to invert the conditional outcome.
Have a look at the below example:

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:
4. Code Reference
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<!DOCTYPE html> <html lang="en"> <head> <title>Two Dimensional Array</title> </head> <body> <script> //Sample 01: Take a Number from User var strTheNumber = prompt("Enter a Number :", 0); var Number = parseInt(strTheNumber, 10); //Sample 02: Using the Simple If Condition if(Number == 0) { document.writeln("The Number is Zero </br>"); } //Sample 03: If and Else if (Number < 0) { document.writeln("The Number is Negative</br>"); } else { document.writeln("The Number is Positive</br>"); } //Sample 04: Multiple Conditions if ( (Number >= 10) && (Number <=25) ) { document.writeln ("Number is in the Range 10 - 25</br>"); } </script> </body> </html> |
Categories: JavaScript
Tags: else, if, Logical Operator