Assignment Operators


Assignment Operators

🙋 Need help? Ask an expert now!

Assignment operators are used to assign a value to a variable. There for many different types of assignment operators, but the good ones to know are listed below.

=,+=, -=, *=, /=

As you can see above, most of the assignment operators are just other operators with an extra equals sign. In fact, the assignment operators can be broken down to individual operators:

a+=b is the same as a = a + b
a-=b is the same as a = a - b
a*=b is the same as a = a * b
a/=b is the same as a = a / b

The statements inside the console.log() statement change the value of x before printing it on the console. As explained in the paragraph above, x -= 1 is essentially x = x - 1, which is why the second print statement prints 5. The rest of the assignment operators work the same way. These assignment operators are good to know and extremely useful when writing programs that require quick calculations and efficient code. You can research and use other assignment operators such as %= after you master the ones above.

Edit Me on GitHub!

Application Question

Consider the following code segment:

let varOne = 20;
varOne += 5;
varOne = 35;
varOne /= 5;

What is the value of varOne after the statements above are executed?