CD-42 Part II . JavaScript Tutorial (Web server setup) Even though
CD-42 Part II . JavaScript Tutorial 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). The decimal and everything to its right are simply stripped off. The parseFloat() function returns an integer if it can; otherwise, it returns a floating-point number as follows: parseFloat( 42 ) // result = 42 parseFloat( 42.33 ) // result = 42.33 Because these two conversion functions evaluate to their results, you simply insert the entire function wherever you need a string value converted to a number. Therefore, modifying an earlier example in which one of three values was a string, the complete expression can evaluate to the desired result: 3 + 3 + parseInt( 3 ) // result = 9 Converting numbers to strings You ll have less need for converting a number to its string equivalent than the other way around. As you saw in the previous section, JavaScript gravitates toward strings when faced with an expression containing mixed data types. Even so, it is good practice to perform data type conversions explicitly in your code to prevent any potential ambiguity. The simplest way to convert a number to a string is to take advantage of JavaScript s string tendencies in addition operations. By adding an empty string to a number, you convert the number to its string equivalent: ( + 2500) // result = 2500 ( + 2500).length // result = 4 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. A string is a JavaScript object that has properties associated with it. 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. Note that the length value is a num ber, not a string. Operators You will use lots of operators in expressions. Earlier, you used the equal sign (=) as an assignment operator to assign a value to a variable. In the preceding exam ples with strings, you used the plus symbol (+) to join two strings. An operator gen erally performs some kind of calculation (operation) or comparison with two values (the value on each side of an operator is called an operand) to reach a third value. In this lesson, I briefly describe two categories of operators arithmetic and com parison. Chapter 40 covers many more operators, but once you understand the basics here, the others are easier to grasp.
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.