The one and only string operator is actually the addition
operator. As shown in the addition operators section, the
addition operator is also a string concatentor. With it you can
combine two strings together, the second to the end of the
first. This has many practical uses, as you'll see as your
skills develop. If a number and a string are being used, the
string is converted to a number, then added to the other number.
If this conversion is not possible, NaN is returned. See the
below example to get a feel for string concatenation.
variableOne = new String("50");
variableTwo = variableOne + 20;
document.write("The contents of variableOne is " + variableOne);
document.write("The contents of variableTwo is " + variableTwo);
The example shows string concatenation, since every one
understands simple addition. variableOne has the string "50"
loaded into it, and variableTwo is the concatenation of the
variableOne value and the number 20. The result of the first
document.write statement is 50, while the result of the second
document.write statement is 5020, the two values, concatenated.
Assignment Operators
The assignment operators are used to assign values to variables.
The most common assignment operator is the "equals" operator
represented by an equals sign. Check out the example that shows
the assignment of the number 50 to the variable variableOne.
variableOne = 50;
You can see that there's nothing to it - with the equals
assignment operator. The others get a bit more complicated and
delve into the concepts of logic and equivalency. Basically, the
rest of the assignment operators are used to perform algebraic
and Boolean functions. Examine the below list of assignment
operators.
= - Assignment - X=Y - The Assignment operator is perhaps
the simplest and most commonly used of all of the operators. It
is used within a variable, for example, to assign the following
value to the preceding variable name.
+= - Add By Value - X=X+Y - The add by value operator is
also known as the "Addition Assignment" operator. It is used to
add the value on the right of the add by value operator to the
value on the left of the add by value operator, the result of
which replaces the variable value on the left of the operator.
If neither of the operand values are strings or numbers, they
will be converted to numbers. This operator can also be used for
string concatenation. That is, the joining of two words
(strings) or numeric characters together to form one string.
Again, the operand on the right is concatenated to the operand
on the left, and the left operand is overwritten with the new
value. If one of the operands is not a string, it is converted
to a string, then concatenated.
-= - Subtraction Assignment - X=X-Y - The subtraction
assignment operator is used to subtract the number on the right
of the operator from the number stored as a variable to the left
of the operator. The result of the operation overwrites the
value stored in the variable to the left. If either of the
operands is a string, it is converted to a number. If this is
not possible, NaN is returned.
*= - Multiplication Assignment - X=X*Y - The multiplication
assignment operator is used to multiply the number stored as a
variable on the left of the operator by the number to the right
of the operator. The result of the operation overwrites the
value stored in the variable to the left. If either of the
operands is a string, it is converted to a number. If this is
not possible, NaN is returned.
/= - Division Assignment - X=X/Y - The division assignment
operator is used to divide the number stored as a variable on
the left of the operator by the number to the right of the
operator. The result of the operation overwrites the value
stored in the variable to the left. If either of the operands is
a string, it is converted to a number. If this is not possible,
NaN is returned.
%= - Modulus Assignment - X=X%Y - The modulus assignment
operator is used to divide the number stored as a variable on
the left of the operator by the number to the right of the
operator. The remainder of the operation is returned,
overwriting the value stored in the variable to the left. If
either of the operands is a string, it is converted to a number.
If this is not possible, NaN is returned.
<<= - Shift Right with Sign Assignment - X=X<<Y
- The shift right with sign assignment operator looks at the
number to the left of the operator as a 32 bit binary number.
The bits of this binary number are then shifted to the right the
number of places within the variable to the right of the
operator. There are a couple of rules to be examined. If the
original number shifted out of existence is a one, zeros are
filled. If the original number is a zero, ones are filled. These
numbers are filled to keep the binary number at an even 32 bits.
The bits on the right of the binary number are lost. The result
is then converted to a decimal integer and stored in the
variable to the left of the operator.
>>= - Shift Left with Assignment - X=X>>Y - The
shift left with sign assignment operator looks at the number to
the left of the operator as a 32 bit binary number. The bits of
this binary number are then shifted to the left the number of
places within the variable to the right of the operator. There
are a couple of rules to be examined. If the original number
shifted out of existence is a one, zeros are filled. If the
original number is a zero, ones are filled. These numbers are
filled to keep the binary number at an even 32 bits. The bits on
the left of the binary number are lost. The result is then
converted to a decimal integer and stored in the variable to the
left of the operator.
>>>= - Shift Right Zero Fill with Assignment - X=X>>>Y - The
shift right zero fill with sign assignment operator looks at the
number to the left of the operator as a 32 bit binary number.
The bits of this binary number are then shifted to the right the
number of places within the variable to the right of the
operator. Regardless of the sign of the original binary number,
the numbers filled used to "fill" the places now deleted are
zeros. No exceptions. These numbers are filled to keep the
binary number at an even 32 bits. The bits on the right of the
binary number are lost. The result is then converted to a
decimal integer and stored in the variable to the left of the
operator.
&= - Bitwise AND Assignment - X=X&Y - The bitwise AND
assignment looks at the numbers on both sides of the operator as
32 bit binary numbers. The logical AND operation is then carried
out on each bit of the 32 bit binary number. The result is
converted to a decimal integer and stored in the variable to the
left of the operator.
|= - Bitwise OR Assignment - X=X|Y - The bitwise OR
assignment looks at the numbers on both sides of the operator as
32 bit binary numbers. The logical OR operation is then carried
out on each bit of the 32 bit binary number. The result is
converted to a decimal integer and stored in the variable to the
left of the operator.
^= - Bitwise Exclusive OR Assignment - X=X^Y - The bitwise
exclusive OR assignment looks at the numbers on both sides of
the operator as 32 bit binary numbers. The logical exclusive OR
operation is then carried out on each bit of the 32 bit binary
number. The result is converted to a decimal integer and stored
in the variable to the left of the operator.
You can see that there is lot to the assignment operators. With
this abundance of information comes an abundance of abilities -
refer to the JavaScript Language Reference when you're in doubt
as to what each does or even if the operator you have a need for
exists. Chances are it does.