Assignment Operators in JavaScript

Assignment operators in JavaScript are used to assign values. For example:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>
   
   <script>
      let x;

      x = 20;
      document.write(x, "<BR>");
      
      x += 5;
      document.write(x, "<BR>");
   </script>
   
</body>
</html>
Output

List of All Assignment Operators in JavaScript

  • Simple assign (=)
  • Add then assign (+=)
  • Subtract then assign (-=)
  • Multiply then assign (*=)
  • Divide then assign (/=)
  • Modulus then assign (%=)
  • Exponent then assign (**=)

JavaScript Assignment Operators Example

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>
   
   <script>
      let a = 20, b = 3, res;

      res = b;
      document.write(res, "<BR>");
      
      res += 5;
      document.write(res, "<BR>");
      
      res -= 5;
      document.write(res, "<BR>");

      res *= 5;
      document.write(res, "<BR>");
      
      res /= 10;
      document.write(res, "<BR>");
      
      a %= 3;
      document.write(a, "<BR>");
      
      b **= 4;
      document.write(b, "<BR>");
   </script>
   
</body>
</html>
Output

Note - The expression, x += 10 is same as x = x+10. Or x += y is same as x = x+y

JavaScript Online Test


« Previous Tutorial Next Tutorial »