Topic group Related topics
Assign/create operator: =
Assignment-only operator: :=
Arithmetic assignment operators: += –= *= /= %=
x = n
y = x
x += y
Assignment operators are binary operators that assign the value of the operand on the right to the operand on the left.
The standard assignment operator is the equal sign. For example, x = 4 assigns the value 4 to the variable x, and y= x assigns the value of the variable x (which must already have an assigned value) to the variable y. If the variable or property on the left of the equal sign does not exist, it is created.
To prevent the creation of a variable or property if it does not exist, use the assignment-only := operator. This operator is particularly useful when assigning values to properties. If you inadvertently misspell the name of the property with the = operator, a new property is created; your code will run without error, but it will not behave as you intended. By using the := operator, if the property (or variable) does not exist, an error occurs.
The arithmetic assignment operators are shortcuts to self-updating arithmetic operations. For example, the expression x += y means that x is assigned its own value plus that of y(x = x + y).Both operands must already have assigned values, or an error occurs. Thus, if the operand x has already been assigned the value 4 and y has been assigned the value 6, the expression x += y returns 10.