Web hosting provider - 65Chapter 6Programming Fundamentals, Part IBut as the 6
Sunday, April 22nd, 200765Chapter 6Programming Fundamentals, Part IBut as the 6 is about to be added to the 3, JavaScript lets the string-ness of the 3 rule. The 6 is converted to a string, and two string values are joined to yield 63. Most of your concern about data types will focus on performing math operations like theones here. However, some object methods also require one or more parameters of particulardata types. While JavaScript provides numerous ways to convert data from one type toanother, it is appropriate at this stage of the tutorial to introduce you to the two most com- mon data conversions: string to number and number to string. Converting strings to numbersAs you saw in the last section, if a numeric value is stored as a string as it is when enteredinto a form text field your scripts may have difficulty applying that value to a math opera- tion. The JavaScript language provides two built-in functions to convert string representa- tions of numbers to true numbers: parseInt()and parseFloat(). There is a difference between integers and floating-point numbers in JavaScript. Integersarealways whole numbers, with no decimal point or numbers to the right of a decimal. Floating- point numbers, on the other hand, have fractional values to the right of the decimal. By andlarge, JavaScript math operations don t differentiate between integers and floating-point num- bers: A number is a number. The only time you need to be cognizant of the difference is whena method parameter requires an integer because it can t handle fractional values. For exam- ple, parameters to the scroll()method of a window require integer values of the number ofpixels vertically and horizontally you want to scroll the window. That s because you can tscroll a window a fraction of a pixel on the screen. To use either of these conversion functions, insert the string value you wish to convert as aparameter to the function. For example, look at the results of two different string values whenpassed through the parseInt()function: parseInt( 42 ) // result = 42parseInt( 42.33 ) // result = 42Even though the second expression passes the string version of a floating-point number tothe function, the value returned by the function is an integer. No rounding of the value occurshere (although other math functions can help with that if necessary). The decimal and every- thing to its right are simply stripped off. The parseFloat()function returns an integer if it can; otherwise, it returns a floating-pointnumber as follows: parseFloat( 42 ) // result = 42parseFloat( 42.33 ) // result = 42.33Because these two conversion functions evaluate to their results, you simply insert the entirefunction wherever you need a string value converted to a number. Therefore, modifying anearlier example in which one of three values was a string, the complete expression can evalu- ate to the desired result: 3 + 3 + parseInt( 3 ) // result = 9Converting numbers to stringsYou ll have less need for converting a number to its string equivalent than the other wayaround. As you saw in the previous section, JavaScript gravitates toward strings when faced
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision ecommerce web hosting services