Language: EN

programacion-operadores-asignacion

Assignment Operators

Assignment operators are used to assign values to variables. The most common assignment operator, by far, is the = operator.

OperatorExample
Assignment (=)x = 5

This operator

  • Computes the value of the expression to its right
  • Assigns it to the variable on its left

For example, we can assign the value 10 to a variable called x as follows:

x = 10

From that moment on, the variable x contains the value 10 and we can use it in our program.

On the right there can be any expression that evaluates to a value. For example, this would also be valid.

a = 10
x = 2 + 3 + a

Compound Assignment Operators

In addition to the basic assignment operator =, many programming languages offer compound assignment operators, which combine assignment with another arithmetic operation.

OperatorExample
Addition Assignment (+=)x += 3 (equivalent to x = x + 3)
Subtraction Assignment (-=)x -= 2 (equivalent to x = x - 2)
Multiplication Assignment (*=)x *= 4 (equivalent to x = x * 4)
Division Assignment (/=)x /= 2 (equivalent to x = x / 2)
Modulus Assignment (%=)x %= 3 (equivalent to x = x % 3)

These operators are a shorthand way of writing common operations and can help make our code more concise and readable.

Let’s see an example in JavaScript using the += operator:

let x = 5;
x += 3; // equivalent to x = x + 3

console.log(x); // will print 8

In this case, the += operator adds 3 to the current value of the variable x and then assigns the result back to x. As a result, x is updated to 8.

Examples of Assignment Operators in Different Languages

As we said, in practically all languages the assignment operator is the equal sign =.

Similarly, the assignment operators +=, -=, *=, and /= are practically a standard.

For example, this is how assignment operators are used in C++, C#, or Java,

// assignment operator
int x = 5;

// compound operators
x += 3;  // Equivalent to x = x + 3
x -= 2;  // Equivalent to x = x - 2
x *= 4;  // Equivalent to x = x * 4
x /= 2;  // Equivalent to x = x / 2
x %= 7;  // Equivalent to x = x % 7

Whose usage is identical in JavaScript,

// assignment operator
let x = 5;

// compound operators
x += 3;  // Equivalent to x = x + 3
x -= 2;  // Equivalent to x = x - 2
x *= 4;  // Equivalent to x = x * 4
x /= 2;  // Equivalent to x = x / 2
x %= 7;  // Equivalent to x = x % 7

Or, for example, in Python,

# assignment operator
x = 5

# compound operators
x += 3  # Equivalent to x = x + 3
x -= 2  # Equivalent to x = x - 2
x *= 4  # Equivalent to x = x * 4
x /= 2  # Equivalent to x = x / 2
x %= 7  # Equivalent to x = x % 7

As we can see, the use of the operators is identical. In the examples, only the way of declaring the variable changes, which has nothing to do with today’s topic, which is the operators.

Best Practices and Cleanliness Tips Tips

In many languages (not all) assignment operations return true as a result of the assignment.

This leads to a very common mistake, if we get confused and put = instead of ==

if(a = 3) // this is always true
{ }

In this case, we are not comparing a with 3. We are storing 3 in a, and evaluating the result as a condition. Here we have two undesirable effects:

  • We are storing a value that we did not want in a variable
  • In addition, the conditional is always met, because = always returns true.

That is, be careful not to mistakenly put = instead of ==.