Java Reference
In-Depth Information
However, string comparison in JavaScript is case sensitive. So "P" is not the same as "p" . Taking the
preceding example, but changing the variable myName to "paul" , you find that the condition is false
and the code inside the if statement does not execute:
var myName = "paul";
if (myName == "Paul"){
alert("myName is Paul");
}
The >= , > , <= , and < operators work with strings as well as with numbers, but again it is an
alphabetical comparison. So "A" < "B" is true , because A comes before B in the alphabet.
However, JavaScript's case sensitivity comes into play again. "A" < "B" is true , but "a" < "B" is
false . Why? Because uppercase letters are treated as always coming before lowercase letters. Why is
this? Each letter has a code number in the ASCII and Unicode character sets, and the code numbers
for uppercase letters are lower than the code numbers for lowercase letters. This is something to
watch out for when writing your own code.
The simplest way to avoid confusion with different cases is to convert both strings to either
uppercase or lowercase before you compare them. You can do this easily using the toUpperCase()
or toLowerCase() function, which you learn about in Chapter 5.
the switch statement
You saw earlier how the if and else if statements could be used for checking various conditions;
if the first condition is not valid, then another is checked, and another, and so on. However, when
you want to check the value of a particular variable for a large number of possible values, there is
a more efficient alternative, namely the switch statement. The structure of the switch statement is
given in Figure 3-7.
Variable expression being checked
These curly braces mark out
the start and end of the switch
statement's case statements.
Checking for possible values.
If a match is found, then execution
starts below the case statement
and ends at the break statement.
switch ( myName )
{
case “Paul”:
// some code
break;
case “John”:
// some other code
break;
default:
//default code
break;
This code executes when none of
the case statements match.
}
figure 3-7  
 
 
Search WWH ::




Custom Search