HTML and CSS Reference
In-Depth Information
Use these operators to evaluate data and to make decisions. For example, if a website
requires that its users be a minimum age to sign up for an account, the logic on the sign-up
page might include something like this:
if(users age >= minimum age)
{
//allow sign-up.
}
Using if statements
Use the if statement to evaluate state to control the direction in which the code will run. The
if statement can stand alone, as shown in the preceding snippet, or be combined with else to
form more complex constructs:
if(exp1, exp2, exp3…expn){
//true logic
}else {
//false logic
}
This if statement starts on a new line with the keyword if . In parentheses following the if
statement is a series of one or more expressions, separated by logical operators when more
than one expression is provided. The code block immediately following the if statement
conditional expression runs only when the expression evaluates to true. When the expression
evaluates to false, the block immediately following the else keyword runs.
The else keyword is optional. An if statement can exist as a standalone statement when no
logic is available to run when the expression evaluates to false.
EXAM TIP
Two conditional operators are available for checking equality: == (equality operator) and ===
(identity operator). Checking for equality with the == operator will ignore the underlying
data type, whereas the === identity operator will consider data type. Look at the following
example:
var n = 2000, s = '2000';
alert(n == s);
alert(n === s);
The first expression, which uses the equality operator, evaluates to true because the string
is cast to a number for the purpose of the evaluation. The second expression, which uses
the identity operator, evaluates to false because the string '2000' isn't equal to the integer
2000.
 
 
Search WWH ::




Custom Search