Language: EN

programacion-operadores-asignacion

Assignment Operators

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

OperatorExample
Assignment (=)x = 5

This operator

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

For example, we can assign the value 10 to a variable called x in the following way:

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
Assignment with addition (+=)x += 3 (equivalent to x = x + 3)
Assignment with subtraction (-=)x -= 2 (equivalent to x = x - 2)
Assignment with multiplication (*=)x *= 4 (equivalent to x = x * 4)
Assignment with division (/=)x /= 2 (equivalent to x = x / 2)
Assignment with modulo (%=)x %= 3 (equivalent to x = x % 3)

These operators are a shorthand way to write 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 mentioned, 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

Its 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 Tips

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

This leads to a very common error if we mistakenly use = instead of ==

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

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

  • We are storing a value we didn’t want in a variable
  • Additionally, the conditional always evaluates to true because = always returns true.

That is, be careful not to confuse and accidentally write = instead of ==