Language: EN

javascript-operadores-asignacion

Assignment Operators in JavaScript

Assignment operators in JavaScript allow us to assign values to variables

The most common assignment operator is the = operator. This operator is used to assign the value on the right to the variable on the left.

In addition to the basic assignment operator (=), there are compound assignment operators that combine an arithmetic operation with the assignment, simplifying and optimizing the code.

Basic assignment operator (=)

The most common assignment operator is the =, which is used to assign the value on the right side to the variable on the left side.

OperatorNameDescription
=AssignmentAssigns a value to a variable

Let’s see it with an example,

let a = 5; // Assigns the value 5 to variable a

It can also be used to update the value of an existing variable:

let a = 5; // Initial assignment

a = 10; // Value update

Do not confuse the assignment operator = with the equality comparator ==.

Compound assignment operators

In addition to the basic operator, JavaScript offers several compound assignment operators that combine arithmetic operations with the assignment. These operators allow for more compact code.

OperatorNameDescription
+=Add and assignAdds the value to the variable and assigns the result
-=Subtract and assignSubtracts the value from the variable and assigns the result.
*=Multiply and assignMultiplies the variable by a value and assigns the result
/=Divide and assignDivides the variable by a value and assigns the result
%=Modulo and assignCalculates the modulo of the variable by a value and assigns the result

Add and assign (+=)

The += operator adds a value to a variable and assigns the result to that variable.

let a = 5;
a += 3; // a is now 8 (equivalent to a = a + 3)

Subtract and assign (-=)

The -= operator subtracts a value from a variable and assigns the result to that variable.

let a = 10;
a -= 3; // a is now 7 (equivalent to a = a - 3)

Multiply and assign (*=)

The *= operator multiplies a variable by a value and assigns the result to that variable.

let a = 4;
a *= 6; // a is now 24 (equivalent to a = a * 6)

Divide and assign (/=)

The /= operator divides a variable by a value and assigns the result to that variable.

let a = 20;
a /= 4; // a is now 5 (equivalent to a = a / 4)

Modulo and assign (%=)

The %= operator calculates the modulo of a variable by a value and assigns the result to that variable.

let a = 17;
a %= 5; // a is now 2 (equivalent to a = a % 5)

Exponentiation and assign (**=)

The **= operator raises a variable to the power of a value and assigns the result to that variable.

let a = 2;
a **= 3; // a is now 8 (equivalent to a = a ** 3)

Operator precedence

It is important to note that assignment operators have a lower precedence than most other operators in JavaScript. Therefore, expressions on the right side of assignment operators are evaluated first.

let a = 5;
let b = 10;
a += b * 2; // a is now 25 (b * 2 is evaluated first, then a += 20)