JavaScript Math.imul() Method



The JavaScript Math.imul() method accepts two parameters and returns the result of multiplying them as a 32-bit signed integer. This method is often used in bitwise operations or in some scenarios where we need to work with 32-bit integers.

Syntax

Following is the syntax of JavaScript Math.imul() method −

Math.imul(a, b)

Parameters

This method accepts two parameters. The same is described below −

  • a: The first integer to be multiplied.
  • b: The second integer to be multiplied.

Return value

This method returns a numeric value that represents the result of C-like 32-bit multiplication of the provided arguments.

Example 1

In the following example, we are using the JavaScript Math.imul() method to multiply the numbers 5 and 6 −

<html>
<body>
<script>
   const result = Math.imul(5, 6);
   document.write(result);
</script>
</body>
</html>

Output

After executing the above program, it returns 30 as result.

Example 2

Here, we are using the Math.imul() method with negative numbers.

<html>
<body>
<script>
   const result = Math.imul(-5, 4);
   document.write(result);
</script>
</body>
</html>

Output

It returns the low 32 bits of the result of -5 multiplied by 4.

Example 3

In this example, we are performing a bitwise multiplication of two binary numbers (1010 and 1101) using Math.imul() method −

<html>
<body>
<script>
   const a = 0b1010; //10
   const b = 0b1101; //13
   const result = Math.imul(a, b);
   document.write(result);
</script>
</body>
</html>

Output

If we execute the above program, the result will be 40.