Changes

JavaScript Operators

3,181 bytes added, 15:03, 20 April 2007
String Operators
== String Operators ==
 
Once you understand comparison operators the concept of String operators is quite straightforward. The String operators consist of the same comparison operators detailed above. In addition there is one more invaluable operator known as the ''concatenation operator'' represented by the + sign. This operator can be used to build strings by joining together multiple strings or strings and other variables.
 
Let's begin with a simple example using the concatenation operater to build a string from two different strings:
 
<pre>
 
var myString1 = "Hello, ";
var myString2 = "how are you?";
 
document.write ( myString1 + myString2);
 
</pre>
 
The above example declares and initializes two string variables and then concatenates them in the ''write'' statement to create output that reads "Hello, how are you?".
 
We can take this a step further and use concatenation using not just variables:
 
<pre>
 
var myString1 = "Hello, ";
var myString2 = "how are you?";
 
document.write ( myString1 + " John, " + myString2);
 
</pre>
 
In this example the output will read "Hello, John, how are you?"
 
It is also important to understand that concatenation can handle types other than strings. For example when the concatentation operator is used and it encounters a number it will convert that number to a string and join it to the string. For example:
 
<pre>
 
document.write ( "You are visitor number " + 10 );
 
</pre>
 
will output the message "You are visitor number 10". Note, however, that JavaScript evaluates expressions from left to right so if it encounters numbers before it encounters a string it will assume that it has found an arithmetic + rather than a string concatenation +. For example:
 
<pre>
 
document.write ( 10 + 5 + " is my lucky number" );
 
</pre>
 
will cause javaScript to add 10 to 5 to get 15, and then concatenate it with the string to provide a string that reads "15 is my lucky number". Once the string has been encountered in the expression all other + operators are converted to strings and concatenated. In the following example once the string has been found the following operands (i.e the 6 and 1 are converted to strings and joined to the end of the string:
 
<pre>
 
document.write ( 10 + 5 + " is my lucky number. So is " + 6 + 1);
 
</pre>
 
The output from the above will read "15 is my lucky number. So is 61". JavaScript also evaluates expressions from the inner parenthesese to outer parentheses. If you need to perform a calculation after a string appears in an expression, therefore, all you need to do is put ( and ) around the arithemtic expression so that it gets evaluated before the string concatenation:
 
<pre>
 
document.write ( 10 + 5 + " is my lucky number. So is " + (6 + 1));
 
</pre>
 
Remembering that JavaScript will evaluate the expression in the inner parenthesese first we see that it will calculate the value of (6 + 1) before the rest of the expression is evaluated. This will be calculated to be 7 and stored. The rest of the expression will then be evaluated, and then the value 7 will be convered to a string (because it appears to the right of the "my lucky number" value) to create a string that reads "15 is my lucky number. So is 7".