What is Multiplication Assignment Operator (*=) in JavaScript?

The multiplication assignment operator (*=) in JavaScript provides a shorthand way to multiply a variable by a value and assign the result back to the variable. Instead of writing a = a * b, you can simply use a *= b.

Syntax

operand1 *= operand2
// equivalent to: operand1 = operand1 * operand2

The multiplication assignment operator is a binary operator that multiplies the left operand by the right operand and stores the result in the left operand.

Example 1: Basic Usage with Numbers

Here's how the multiplication assignment operator works with numeric values:

<!DOCTYPE html>
<html>
<body>
   <h2>Multiplication Assignment Operator (*=) in JavaScript</h2>
   <p>Enter two numbers:</p>
   <input type="number" id="inp1"><br><br>
   <input type="number" id="inp2"><br><br>
   <button onclick="display()">Show Result</button>
   <p id="result"></p>
   
   <script>
      function display() {
         var inp1 = document.getElementById("inp1");
         var inp2 = document.getElementById("inp2");
         var val1 = Number(inp1.value);
         var val2 = Number(inp2.value);
         
         // Using multiplication assignment operator
         val1 *= val2; // equivalent to: val1 = val1 * val2
         
         document.getElementById("result").innerHTML = "<b>Result: " + val1 + "</b>";
      }
   </script>
</body>
</html>

Example 2: Working with Different Data Types

The multiplication assignment operator handles type conversion automatically when working with strings that contain numeric values:

<!DOCTYPE html>
<html>
<body>
   <h2>Multiplication Assignment with Mixed Types</h2>
   <p>Enter a number and text:</p>
   <input type="number" id="inp1" placeholder="Enter number"><br><br>
   <input type="text" id="inp2" placeholder="Enter text or number"><br><br>
   <button onclick="calculate()">Calculate</button>
   <p id="result"></p>
   
   <script>
      function calculate() {
         var val1 = Number(document.getElementById("inp1").value);
         var val2 = Number(document.getElementById("inp2").value);
         
         val1 *= val2;
         
         document.getElementById("result").innerHTML = "<b>Result: " + val1 + "</b>";
      }
   </script>
</body>
</html>

How It Works

When using the multiplication assignment operator:

  • If both operands are numbers, normal multiplication occurs
  • If a string contains a valid number, JavaScript converts it automatically
  • If a string contains non-numeric characters, the result will be NaN (Not a Number)
  • The operator modifies the original variable, unlike regular multiplication

Common Use Cases

The multiplication assignment operator is commonly used for:

  • Scaling values (e.g., converting units)
  • Calculating compound interest or growth rates
  • Updating counters or accumulators in loops
  • Mathematical operations where you need to modify the original variable

Conclusion

The multiplication assignment operator (*=) provides a concise way to multiply and assign values in JavaScript. It handles type conversion automatically but returns NaN when multiplying numbers with non-numeric strings.

Updated on: 2026-03-15T21:53:03+05:30

713 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements