JavaScript Math.imul()
Syntax & Examples
Math.imul() static-method
The Math.imul() method in JavaScript performs a 32-bit integer multiplication of two numbers. This method is useful for tasks that require high-performance integer arithmetic. It is a static method of Math, meaning it is always used as Math.imul() and not as a method of a Math object created (Math is not a constructor).
Syntax of Math.imul()
The syntax of Math.imul() static-method is:
Math.imul(a, b)
This imul() static-method of Math returns the result of the 32-bit integer multiplication of x and y.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
a | required | The first number to multiply. |
b | required | The second number to multiply. |
Return Type
Math.imul() returns value of type Number
.
✐ Examples
1 Using Math.imul() with positive integers
In this example, we use the Math.imul()
method to multiply two positive integers, 2 and 3.
For example,
- We call
Math.imul()
with 2 and 3 as the arguments. - The method returns the 32-bit integer multiplication of 2 and 3.
- We log the result to the console using
console.log()
.
JavaScript Program
console.log(Math.imul(2, 3));
Output
6
2 Using Math.imul() with a positive and a negative integer
In this example, we use the Math.imul()
method to multiply a positive integer, 2, and a negative integer, -3.
For example,
- We call
Math.imul()
with 2 and -3 as the arguments. - The method returns the 32-bit integer multiplication of 2 and -3.
- We log the result to the console using
console.log()
.
JavaScript Program
console.log(Math.imul(2, -3));
Output
-6
3 Using Math.imul() with large numbers
In this example, we use the Math.imul()
method to multiply two large numbers, 123456 and 789012.
For example,
- We call
Math.imul()
with 123456 and 789012 as the arguments. - The method returns the 32-bit integer multiplication of 123456 and 789012.
- We log the result to the console using
console.log()
.
JavaScript Program
console.log(Math.imul(123456, 789012));
Output
1942908512
Summary
In this JavaScript tutorial, we learned about imul() static-method of Math: the syntax and few working examples with output and detailed explanation for each example.