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.