Assignment Operators in JavaScript

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

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>
   
   <p id="abc"></p>
   <p id="xyz"></p>

   <script>
      let x;

      x = 20;
      document.getElementById("abc").innerHTML = x;
      
      x += 5;
      document.getElementById("xyz").innerHTML = x;
   </script>
   
</body>
</html>
Output

List of All Assignment Operators in JavaScript

JavaScript Assignment Operators Example

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>

   <p id="para1"></p>
   <p id="para2"></p>
   <p id="para3"></p>
   <p id="para4"></p>
   <p id="para5"></p>
   <p id="para6"></p>
   <p id="para7"></p>
   
   <script>
      let a = 20, b = 3, res;

      res = b;
      document.getElementById("para1").innerHTML = res;
      
      res += 5;
      document.getElementById("para2").innerHTML = res;
      
      res -= 5;
      document.getElementById("para3").innerHTML = res;

      res *= 5;
      document.getElementById("para4").innerHTML = res;
      
      res /= 10;
      document.getElementById("para5").innerHTML = res;
      
      a %= 3;
      document.getElementById("para6").innerHTML = a;
      
      b **= 4;
      document.getElementById("para7").innerHTML = b;
   </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 »