# Assignment Operators

In JavaScript, the following operators can be use to assign values to variables:

For Text

1. The = equals sign:
• var message1 = "Hello World!"
var message2 = message1

message2 is now "Hello World!"

2. The += (plus equals), a shorthand operator that concatenates and assigns a value simultaneously:
• var message1 = "Mc"
var message2 = "Donalds"
message2 = message1 + message2

message2 is now "McDonalds"

Using +=, this can also be achieved with:

message1 += message2

For Numbers

1. with numbers += is a shorthand for adding two values together and assigning the result to the variable to the left of the equals sign
• var x = 2
var y += 3

var y is now 5

2. There are a number of additional operator shorthands that follow this formula than can be used on numbers as listed in this chart:
3. operatoroperation
x -= yx = x - y
x *= yx = x * y
x /= yx = x / y
4. And some obscure ones that you probably won't have much use for:
5. operatoroperation
x %= yx = x % y
x <<= yx = x << y
x >>= yx = x >> y
x >>>= yx = x >>> y
x &= yx = x & y
x ^= yx = x ^ y
x |= yx = x | y
6. There is even a shorthand for a shorthand! The ++ increment operator is a shorthand for += 1
• var x = 1
++x

x is now 2

7. Similary, the -- decrement operator is a shorthand for -= 1
var x = 2
--x

x is now 1

8. The increment and decrement operators can be placed before or after a number, and produce the same result.
9. ++x and x++ are equivalent, if the assignment is the entire statement

x = 1
++x
x++

x is now 3

However, the increment and decrement operators can also be used to make an assignment within a statement, which is why they really exist, and at this point have a different meaning. When the operators precede the value, the value is incremented before the rest of the statement is executed. When the operators follow the value, the value is incremented after the rest of the statement is executed.

x = 1
y = ++x

x is now 2
y is now 2

However

x = 1
y = x++

x is now 2
but y is 1