66Part IIJavaScript (Web hosting reviews) Tutorialwith an expression containing mixed data
66Part IIJavaScript Tutorialwith an expression containing mixed data types. Even so, it is good practice to perform datatype conversions explicitly in your code to prevent any potential ambiguity. The simplest wayto convert a number to a string is to take advantage of JavaScript s string tendencies in addi- tion operations. By adding an empty string to a number, you convert the number to its stringequivalent: ( + 2500) // result = 2500 ( + 2500).length // result = 4In the second example, you can see the power of expression evaluation at work. The paren- theses force the conversion of the number to a string. A stringis a JavaScript object that hasproperties associated with it. One of those properties is the lengthproperty, which evalu- ates to the number of characters in the string. Therefore, the length of the string 2500 is 4. Note that the length value is a number, not a string. OperatorsYou will use lots of operatorsin expressions. Earlier, you used the equal sign (=) as an assign- ment operator to assign a value to a variable. In the preceding examples with strings, youused the plus symbol (+) to join two strings. An operator generally performs some kind of cal- culation (operation) or comparison with two values (the value on each side of an operator iscalled an operand) to reach a third value. In this lesson, I briefly describe two categories ofoperators arithmetic and comparison. Chapter 32 covers many more operators, but onceyou understand the basics here, the others are easier to grasp. Arithmetic operatorsIt may seem odd to talk about text strings in the context of arithmetic operators, but youhave already seen the special case of the plus (+) operator when one or more of the operandsis a string. The plus operator instructs JavaScript to concatenate(pronounced kon-KAT-en- eight), or join, two strings together precisely where you place the operator. The string con- catenation operator doesn t know about words and spaces, so the programmer must makesure that any two strings to be joined have the proper word spacing as part of the strings even if that means adding a space: firstName = John ; lastName = Doe ; fullName = firstName + + lastName; JavaScript uses the same plus operator for arithmetic addition. When both operands arenumbers, JavaScript knows to treat the expression as an arithmetic addition rather than astring concatenation. The standard math operators for addition, subtraction, multiplication, and division (+, -, *, /) are built into JavaScript. Comparison operatorsAnother category of operator helps you compare values in scripts whether two values arethe same, for example. These kinds of comparisons return a value of the Boolean type trueor false. Table 6-2 lists the comparison operators. The operator that tests whether twoitems are equal consists of a pair of equal signs to distinguish it from the single equal signassignment operator.
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision j2ee hosting services