Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

4.2 - Assignment Operators

assignment operators

OperatorMeaningExample
=Equals: Assigns a valuex = y
+=Plus Equals: Assigns the original value plus the new valuex += y // x = x + y
-=Minus Equals: Assigns the original value minus the new valuex -= y // x = x - y
*=Multiply Equals: Assigns the original value times the new valuex *= y // x = x * y
/=Divide Equals: Assigns the original value divided by the new valuex /= y // x = x / y
%=Modulo Equals: Assigns the original value modulo the new valuex %= y // x = x % y
++Increases the variable by 1x++
--Decreases the variable by 1x--

code examples

x = 10;
x += 3; // shorthand for x = x + 3
console.log(x); // should output 13 since 10 + 3 = 13

x = 10;
x /= 5; // shorthand for x = x ÷ 5
console.log(x); // outputs 2, since 10 ÷ 5 = 2

x = 10;
x++; 
console.log(x); // outputs 11, since 10 + 1 = 11

assignment operators

  1. With your existing number variables, apply some assignment operators to them.
  2. console.log() the output of your variables after applying these operators!