JavaScript Math.imul() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

Math.imul(a, b) multiplies two numbers as 32-bit integers and returns a signed 32-bit result. If the answer is too big, it wraps around instead of becoming a huge number.

Math is built into JavaScript. Call the method directly. It is not a constructor, so never write new Math().

Syntax

Call Math.imul() like this. Start with this line.

JavaScript
Math.imul(a, b)
CallWhat it does
Math.imul(a, b)Returns a * b as a signed 32-bit integer.

Built into JavaScript. Call Math.imul() on the Math object — no import required.

Library declarations
JavaScript
Math.imul(a, b)
JavaScript
interface Math {
  imul(x: number, y: number): number;
}

Parameters

ParameterDescription
aRequired. The first number. It is converted to a 32-bit integer.
bRequired. The second number. It is converted to a 32-bit integer.

Return Value

Return ValueDescription
numberThe product as a signed 32-bit integer, from -2147483648 to 2147483647.

JavaScript Examples

Example 1 — Basic Use

For small numbers, Math.imul() gives the same answer as *.

JavaScript
console.log(Math.imul(3, 4));     // 12
console.log(Math.imul(-5, 12));   // -60
console.log(Math.imul(2.9, 3));   // 6
console.log(Math.imul(0, 100));   // 0
Try It Yourself

How It Works

  1. Small numbers. 3 * 4 fits easily, so the answer is 12.
  2. Negative numbers. The sign is kept: -60.
  3. Decimals. 2.9 is cut to 2, so the result is 6.
  4. Zero. Anything times 0 is 0.

Example 2 — Overflow Wraps Around

When the answer is too big for 32 bits, Math.imul() wraps. The * operator does not.

JavaScript
console.log(65536 * 65536);              // 4294967296
console.log(Math.imul(65536, 65536));    // 0

console.log(2147483647 * 2);             // 4294967294
console.log(Math.imul(2147483647, 2));   // -2
Try It Yourself

How It Works

  1. Normal multiply. 65536 * 65536 is 4294967296, a number bigger than 32 bits.
  2. imul wraps. In 32 bits that value is 0.
  3. Sign flip. 2147483647 * 2 wraps into the negative range and becomes -2.

Example 3 — Simple String Hash

A classic hash multiplies by 31 for every character. Math.imul() keeps the number at 32 bits.

JavaScript
function hash(text) {
  var h = 0;
  for (var i = 0; i < text.length; i++) {
    h = (Math.imul(31, h) + text.charCodeAt(i)) | 0;
  }
  return h;
}

console.log(hash("hello"));  // 99162322
console.log(hash("Hello"));  // 69609650
Try It Yourself

How It Works

  1. Start at 0. The hash h begins as 0.
  2. Mix each letter. Multiply by 31 with Math.imul, then add the letter code.
  3. Keep 32 bits. | 0 makes sure the value stays a signed 32-bit number.
  4. Different input. A small change gives a very different hash.

Browser & Runtime Support

Math.imul() is part of ES2015 and is Baseline Widely available in modern browsers and Node.js.

✓ Baseline · ES2015

Math.imul()

Supported in Chrome, Firefox, Safari, Edge, and Node.js for years. Safe for modern apps without a polyfill.

98% Modern browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
Math.imul Excellent

Bottom line: Use Math.imul() in new code. Only very old browsers, like Internet Explorer, lack it.

Frequently Asked Questions

Math.imul(a, b) multiplies a and b as 32-bit integers and returns a signed 32-bit result. Math.imul(3, 4) is 12.
The * operator keeps big results. Math.imul() wraps around at 32 bits. For example, 65536 * 65536 is 4294967296, but Math.imul(65536, 65536) is 0.
It is handy for hash functions and other code that needs exact 32-bit integer math, like in other languages such as C or Java.
Decimals are cut off first. Math.imul(2.9, 3) works like 2 * 3, which is 6.
It was added in ES2015 and works in all modern browsers and Node.js.

Next: log()

Learn how to find the natural logarithm.

log() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful