HTML and CSS Reference
In-Depth Information
Figure 5.5 Output from Example 5.5. Auto-increment and auto-decrement operators.
5.2.4 Concatenation Operator
As shown in previous examples, the + sign is used for concatenation and addition. The
concatenation operator, the + sign, is a string operator used to join together one or more
strings. In fact, the concatenation operator is the only operator JavaScript provides to
manipulate strings.
In the example, “correct” + “tion” + “al” , the result is “correctional” . If the operands
are a mix of strings and numbers, JavaScript will convert the numbers to strings . For
example, “22” + 8 results in “228” , not 30 . If the operands are numbers, then the + sign
is the addition operator as in 5 + 4 . But suppose we say, “22” * 1 + 4 . In this case, Java-
Script sees the multiplication operator (*) and converts the string “22” to a number,
resulting in 22 + 4 or 26 . Firefox provides the JavaScript console for testing these expres-
sions or you can type JavaScript: in the URL, followed by the expression you want to test,
as shown in Figures 5.6 and 5.7.
The concatenation operator is summarized in Table 5.5. To explicitly convert strings
to numbers, JavaScript provides built-in functions called parseInt() and parseFloat() ,
discussed in Sections 5.3.1 and 5.3.2, respectively.
Figure 5.6 Evaluating expressions in the JavaScript: URL. The result of the test, 26 ,is
displayed in the browser window.
 
 
Search WWH ::




Custom Search