« Home « Kết quả tìm kiếm

JavaScript Bible, Gold Edition part 12


Tóm tắt Xem thử

- I mentioned earlier that the type of data in an expression can trip up some script operations if the expected components of the operation are not of the right type..
- You can begin experimenting with the way JavaScript evaluates expressions with the help of The Evaluator Jr.
- Using the age variable examples from earlier in this chapter, type each of the following statements into the upper text box and observe how each expression evaluates in the Results field.
- A case in point is adding numbers that may be in the form of text strings.
- Therefore, in the statement.
- result = “33”.
- the “string-ness” of the second value prevails over the entire operation.
- Try this yourself in The Evaluator Jr..
- If I take this progression one step further, look what happens when another num- ber is added to the statement:.
- result = “63”.
- But as the 6 is about to be added to the “3,” JavaScript lets the.
- “string-ness” of the “3” rule.
- While JavaScript provides numerous ways to convert data from one type to another, it is appropriate at this stage of the tutorial to intro- duce you to the two most common data conversions: string to number and number to string..
- As you saw in the last section, if a numeric value is stored as a string — as it is when entered into a form text field — your scripts will have difficulty applying that value to a math operation.
- Integers are always whole numbers, with no decimal point or numbers to the right of a decimal.
- Floating-point numbers, on the other hand, can have fractional values to the right of the decimal.
- The only time you need to be cognizant of the difference is when a method parameter requires an integer because it can’t handle fractional values.
- For example, parame- ters to the scroll() method of a window require integer values of the number of pixels vertically and horizontally you want to scroll the window.
- To use either of these conversion functions, insert the string value you wish to convert as a parameter to the function.
- Even though the second expression passes the string version of a floating-point number to the function, the value returned by the function is an integer.
- No round- ing of the value occurs here (although other math functions can help with that if necessary).
- Therefore, modifying an earlier example in which one of three values was a string, the complete expression can evaluate to the desired result:.
- As you saw in the previous section, JavaScript gravitates toward strings when faced with an expression containing mixed data types.
- result = “2500”.
- In the second example, you can see the power of expression evaluation at work..
- The parentheses force the conversion of the number to a string.
- One of those properties is the length property, which evaluates to the number of characters in the string..
- Therefore, the length of the string “2500” is 4.
- In the preceding exam- ples with strings, you used the plus symbol.
- It may seem odd to talk about text strings in the context of “arithmetic” opera- tors, but you have already seen the special case of the plus.
- operator when one or more of the operands is a string.
- The string concatenation operator doesn’t know about words and spaces, so the programmer must make sure that any two strings to be joined have the proper word spacing as part of the strings — even if that means adding a space:.
- These kinds of comparisons return a value of the Boolean type — true or false .
- Where comparison operators come into greatest play is in the construction of scripts that make decisions as they run.
- A cook does this in the kitchen all the time:.
- You see comparison operators in action in the next chapter..
- Which of the following are valid variable declarations or initializations?.
- var zipCode = document.form1.zip.value.
- var 1address = document.nameForm.address1.value 2.
- For each of the statements in the following sequence, write down how the.
- How do you fix the script so the proper sum is displayed in the output field?.
- <INPUT TYPE=”text” NAME=”inputA” VALUE=”0” SIZE=4><BR>.
- <INPUT TYPE=”text” NAME=”inputB” VALUE=”0” SIZE=4>.
- <INPUT TYPE=”button” VALUE=”Add” onClick=”addIt()”>.
- What does the term concatenate mean in the context of JavaScript programming?.
- Before you’re finished here, you will learn how to use one of the most powerful information holders in the JavaScript language: the array..
- Every waking hour of every day you make decisions of some kind — most of the time you probably don’t even realize it.
- Don’t think so? Well, look at the number of decisions you make at the grocery store, from the moment you enter the store to the moment you clear the checkout aisle..
- Based on the number and size of items you intend to buy, do you pick up a hand-carried basket or attempt to extri- cate a shopping cart from the metallic conga line near the front of the store? That key decision may have impact later when you see a special offer on an item that is too heavy to put into the hand basket..
- otherwise, you skip the aisle and move to the head of the next aisle..
- Standing in front of the bin of tomatoes, you begin inspecting them one by one — picking one up, feeling its firm- ness, checking the color, looking for blemishes or signs of pests.
- Your last stop in the store is the checkout aisle.
- “Paper or plastic?” the clerk asks.
- What you choose impacts how you get the groceries from the car to the kitchen as well as your recycling habits..
- In your trip to the store, you go through the same kinds of decisions and repeti- tions that your JavaScript programs also encounter.
- Control Structures.
- In the vernacular of programming, the kinds of statements that make decisions and loop around to repeat themselves are called control structures.
- An important part of a control structure is the condition.
- Three of the most common control structures you’ll use are if construc- tions, if...else constructions, and for loops..
- The simplest program decision is to follow a special branch or path of the pro- gram if a certain condition is true.
- In the parentheses goes an expression that evaluates to a Boolean value.
- This is the condition being tested as the program runs past this point.
- If the condition evaluates to true , then one or more statements inside the curly braces execute before continuing on with the next statement after the closing brace.
- If the condition evaluates to false , then the statements inside the curly brace are ignored and processing continues with the next statement after the clos- ing brace..
- The following example assumes that a variable, myAge , has had its value set earlier in the script (exactly how is not important for this example).
- The condition expression compares the value myAge against a numeric value of 18..
- The data type of the value inside myAge must be a number so that the proper comparison (via the <.
- For all instances of myAge less than 18, the nested statement inside the curly braces runs and displays the alert to the user.
- In the plain if construction, no special processing is performed when the condition evaluates to false .
- Everything you know about the condition for an if construction applies here..
- The only difference is the else keyword, which provides an alternate path for exe- cution to follow if the condition evaluates to false.
- (True testing for this value includes special treatment of end-of-century dates, but I’m ignoring that for now.) The % operator symbol is called the modulus operator (covered in more detail in Chapter 40).
- The result of an operation with this operator yields the remainder of division of the two values.
- The important point to see from this example is that by the end of the if...else construction, the febDays variable is set to either 28 or 29.
- Processing then picks up with the next statement after the if...else construction.

Xem thử không khả dụng, vui lòng xem tại trang nguồn
hoặc xem Tóm tắt