Javascript - Loops - Javascript If Else
Mayıs 12, 2011 by Javascript
|
|
Article Information
This Javascript tutorial contains and tries to cover following subjects:
- Explanation of Javascript IF ELSE
- Example to Javascript If Else in Javascript
Articles tries to provide answer to following questions and issues:
- How to check a condition to execute a code, and if condition does not match executing another code in Javascript
- How to use Else IF in Javascript
Articles pre-requisites following information:
- General knowledge of variables in Javascript
- General knowledge of functions in Javascript
- General knowledge of HTML
- General knowledge of Conditional Statements in Javascript
Brief explanation of Javascript Array
In this javascript tutorial, we will try to cover Javascript If Else to understand how to use, to check a condition for executing a block of code, and if condition is false then executing another code block.
IF we recall earlier example, we had used IF to check a condition, and depending its evaluation TRUE - FALSE, we had executed a code block. Lets assume that we want to execute two different code line depending on a condition. In normal case, we can achieve this with adding to IF block to our code. One If checks if condition true, next if checks opposing of condition is true. In logical view, two if works. However, there is an easier way to check such conditions in javascript. Lets look into two if solution:
if (condition)
{
...execute some codes...
}
if (! (condition) )
{
...execute some codes...
}
In this javascript if method, If a condition returns TRUE, reverse of guarantees a return of FALSE. If we recall, "!" javascript operator used to mean reverse of condition. With that way, we could execute two different code depending on incoming boolean condition.
That two if condition check was a method. Javascript provides another easier solution to that conditional code execution. IF ELSE works same way. IF checks a condition, if TRUE is evaluated, then code is executed following the IF. If condition evaluates FALSE, ELSE block is executed.
if (condition)
{
execute some codes....
}
else
{
execute some codes....
}
Another point of view to ELSE: when we use two IF to test a condition, notice that Javascript executes two test to check condition. In ELSE, first IF is tested, if FALSE, ELSE is executed without a test. That makes javascript code simpler, easier to read and brings better performance.
ELSE can be nested in a IF with additional IF tests if it is required. ELSE IF example-syntax:
if (condition)
{
execute some codes....
}
else if (condition)
{
execute some codes....
}
else
{
execute some codes....
}
In order to see javascript IF ELSE in example, we will use it in a html page in example.
Example to Javascript IF ELSE
Following example demonstrates javascript IF ELSE and ELSE IF in html.
In above javascript else if example, we created a variable and assigned its value "2". If statement tested first condition, tested second condition as else if and finally executed ELSE due to two FALSE from earlier 2 test.
Data Layers
Area: | programming \ languages \ javascript \ \ \ |
Ref: | |
Loc: | articles |
Tags: | javascript |
|