Overview
Assignment Operators are of two types. Simple assignment operator, and Compound assignment operator.
The simple assignment operator assigns the value of the right operand to the variable (or the field) specified in the left operand.
The following is a simple assignment operator:
- = (Simple assignment operator. Assigns values from right side operands to left side operand)
Example:
x = 2; // assigns the value 2 to x
Compound assignment operators consist of a binary operator and the simple assignment operator. They perform the operation of the binary operator on both operands and store the result of that operation into the left operand.
The following are compound assignment operators:
- += (Addition assignment operator. It adds the right operand to the left operand and assign the result to the left operand)
Example:
x += 1; // is the same as: x = x + 1; - -= (Subtraction assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand)
Example:
x -= 1; // is the same as: x = x - 1; - *= (Multiplication assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand)
Example:
x *= 1; // is the same as: x = x * 1; - /= (Division assignment operator. It divides the left operand with the right operand and assigns the result to the left operand)
Example:
x /= 1; // is the same as: x = x / 1; - %= (Modulus assignment operator. It takes modulus using two operands and assigns the result to the left operand)
Example:
x %= 1; // is the same as: x = x % 1;
Data types applicable to Assignment Operators
- The "=" operator supports all datatypes.
- The "+=" operator supports string, bigint and decimal data types.
- The rest of the assignment operators support bigint and decimal datatypes.