if-then-else In our daily life we frequently come across conditional work. For example, suppose you make a guard stand at the entrance of some hall and tell him to ask every trespasser his name. "If the name is 'Ali', let him enter; else deny entrance." Re-read the last sentence. It is one of the best examples of conditional constructs. In programming, too, we come across conditional code. For example, you ask the visitor to your site his name. If the name is 'Ali', let him visit; else deny entrance. Although not a working code, following snippet will make the syntax clear. Remember, the equality in JavaScript is checked with "==" rather than a single equal sign. if (name == "Ali") Note a few things: the conditional construct starts with an if keyword. Then comes the condition part enclosed between brackets. Here we are checking for equality. That is why, the equal sign == has been used. We can check for a lot of other things in an if condition. You will see that part later. Next, there is some code enclosed between curly brackets. This code will be executed if the condition name == "Ali" is satisfied. Otherwise, the code following the keyword else will be executed. I think that's enough for now. We will see a working code example, but first let's review some HTML from tags.
{
// do something
}
else
{
// do something else
}
Go To Page: 1 2