in black and white
Main menu
Share a book About us Home
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics

Javascript for dummies 4th edition - Veer E.V

Veer E.V Javascript for dummies 4th edition - Wiley publishing , 2004. - 387 p.
ISBN: 0-7645-7659-3
Download (direct link): javascriptfordummies2005.pdf
Previous << 1 .. 13 14 15 16 17 18 < 19 > 20 21 22 23 24 25 .. 118 >> Next

You're running Netscape on the Web page.
• If the second condition is false, the JavaScript interpreter displays
You're not running Microsoft IE or Netscape on the Web page.
Chapter 3: JavaScript Language Basics
You might notice that Listing 3-1 contains two if-else statements, one nested inside the other. Technically speaking, you can nest as many if-else statements as you want. If you run across a situation in which you need more than one or two nested if-else statements to do the job, however, you might want to consider the switch statement (which I describe in the next section) instead. The switch statement is much more efficient at testing a condition multiple times.
Some JavaScript programmers end each statement with a semicolon, like this:
if (a == b) { // if a is equal to b
c = d; // assign the value of d to the c variable,
e = f; // assign the value of f to the e variable,
// and assign the string "American Beauty”
// to the variable called favoriteMovie favoriteMovie = "American Beauty”;
Semicolons are optional in JavaScript, with one exception. If you place more than one JavaScript statement on the same line, you must separate those statements with semicolons. For example:
// Wrong!
c = d e = f favoriteMovie = "American Beauty”
// Correct (if a bit hard to read) c = d; e = f; favoriteMovie = "American Beauty”;
The switch statement provides an easy way to check an expression for a bunch of different values without resorting to a string of if-else statements.
Here’s the syntax:
switch (expression) { case label : statement break case label : statement break
default : statement
Suppose you want to examine a value and find out whether it matches one of a number of predefined values. Listing 3-2 shows how you can go about it by using the switch statement.
40 Part I: Building Killer Web Pages for Fun and Profit
Listing 3-2: Using the switch Statement to Match Values
switch (month) {
case 0 :
disp ayMonth = ’January”
case 1 :
disp ayMonth = ’February”
case 2 :
disp ayMonth = ’March”
brea case 3
disp ayMonth = ’April”
case 4 :
disp ayMonth = ’May”
case 5 :
disp ayMonth = ’June”
case 6 :
disp ayMonth = ’July”
case 7 :
disp ayMonth = ’August”
case 8 :
disp ayMonth = ’September”
case 9 :
disp ayMonth = ’October”
case 10 :
disp ayMonth = ’November”
case 11 :
disp ayMonth = ’December”
default: } displayMonth = "INVALID”
The code shown in Listing 3-2 tests the value of the month variable. If month contains the number 0, the variable displayMonth is set to January. If month contains the number 1, displayMonth is set to February — and so on, all the way through the 12 months of the year.
Chapter 3: JavaScript Language Basics
The companion CD contains a date_and_time_formatted.htm file, a working copy of the script in Listing 3-2.
Note that if you forget to finish each case with a break statement (and it’s easy to do), the interpreter falls through, meaning that it performs all the statements that it finds until it either
^ Finds a break
^ Detects the end of the switch statement
For instance, in Listing 3-2, if you removed all the break statements, a month value of 0 would cause displayMonth to be set not to January, as it should be, but to INVALID instead.
In some cases, you may want to leave out the break statement on purpose to force the JavaScript interpreter to fall through two or more cases. Doing so allows you to group values easily. For example, the following code treats month values of 0, 1, or 2 (which correspond to January, February, and March, respectively) the same, by assigning the value Q1 to the displayQuarter variable. Months 3, 4, and 5 (April, May, and June, respectively) are treated the same, by assigning the value Q2 to the displayQuarter variable; and so on.
switch (monthId) {
case 0:
case 1:
case 2:
displayQuarter = ”Q1”;
case 3:
case 4:
case 5:
displayQuarter = "Q2";
case 6:
case 7:
case 8:
displayQuarter = "Q3";
case 9:
case 10:
case 11:
displayQuarter = "Q4";
break; }
Part I: Building Killer Web Pages for Fun and Profit
Fully functioning
A function is a named group of JavaScript statements that you can declare once, near the top of your script, and call over and over again. Adding a reusable function to your script — instead of adding several slightly different versions of the same code — cuts down on the amount of typing that you need to do (yay!), as well as the number of potential bugs in your script (double yay!).
Organizing your script into functions, like organizing your closet, might seem like loads of up-front work for nothing — after all, you don’t have to do it. Your script and your closet can be functional even if they’re messy. The payoff comes when you have to quickly find a problem (or the perfect brown leather belt) hiding somewhere in all that confusion!
Previous << 1 .. 13 14 15 16 17 18 < 19 > 20 21 22 23 24 25 .. 118 >> Next