[ Pobierz całość w formacie PDF ]
.Forexample, parameters to the moveTo() method of a window require integer valuesof the coordinates where you want to position the window.That s because youcan t move an object a fraction of a pixel on the screen.To use either of these conversion functions, insert the string value you wish toconvert as a parameter to the function.For example, look at the results of twodifferent string values when passed through the parseInt() function:parseInt( 42 ) // result = 42parseInt( 42.33 ) // result = 42Even though the second expression passes the string version of a floating-pointnumber to the function, the value returned by the function is an integer.Norounding of the value occurs here (although other math functions can help withthat 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 afloating-point number: Part II JavaScript Tutorial68parseFloat ( 42 ) // result = 42parseFloat ( 42.33 ) // result = 42.33Because these two conversion functions evaluate to their results, you simplyinsert the entire function wherever you need a string value converted to a string.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 = 9Converting numbers to stringYou ll have less need for converting a number to its string equivalent than theother way around.As you saw in the previous section, JavaScript gravitates towardstrings when faced with an expression containing mixed data types.Even so, it isgood practice to perform data type conversions explicitly in your code to preventany potential ambiguity.The simplest way to convert a number to a string is totake advantage of JavaScript s string tendencies in addition operations.By addingan empty string to a number, you convert the number to its string equivalent:(  + 2500) // result =  2500(  + 2500).length // result = 4In 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 aJavaScript object that has properties associated with it.One of those properties isthe 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 anumber, not a string.OperatorsYou 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 previous exampleswith strings, you used the plus symbol (+) to join (concatenate) two strings.Anoperator generally performs some kind of calculation (operation) or comparisonwith two values to reach a third value.In this lesson I briefly describe twocategories of operators  arithmetic and comparison.Many more operators arecovered in Chapter 32, but once you understand the basics here, the others will beeasier to grasp when you re ready for them.Arithmetic operatorsThe string concatenation operator doesn t know about words and spaces, so theprogrammer must make sure that any two strings to be joined have the properword spacing as part of the strings, even if that means adding a space:firstName =  JohnlastName =  DoefullName = firstName +   + lastName Chapter 6 Programming Fundamentals, Part I69JavaScript uses the same plus operator for arithmetic addition.When bothvalues on either side of the plus sign are numbers, JavaScript knows to treat theexpression as an arithmetic addition rather than a string concatenation.Thestandard math operators for addition, subtraction, multiplication, and division (+,-, *, /) are built into JavaScript.Comparison operatorsAnother category of operator helps you compare values in scripts  whethertwo values are the same, for example.These kinds of comparisons return a valueof the Boolean type  true or false.Table 6-2 lists the comparison operators.The operator that tests whether two items are equal consists of a pair of equalssigns to distinguish it from the single equals sign assignment operator.Table 6-2JavaScript Comparison OperatorsSymbol Description== Equals!= Does not equal> Is greater than>= Is greater than or equal to [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • wpserwis.htw.pl