Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. Math
  6. imul()

Math.imul()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨July 2015⁩.

TheMath.imul() static method returns the result of the C-like 32-bit multiplication of the two parameters.

Try it

console.log(Math.imul(3, 4));// Expected output: 12console.log(Math.imul(-5, 12));// Expected output: -60console.log(Math.imul(0xffffffff, 5));// Expected output: -5console.log(Math.imul(0xfffffffe, 5));// Expected output: -10

Syntax

js
Math.imul(a, b)

Parameters

a

First number.

b

Second number.

Return value

The result of the C-like 32-bit multiplication of the given arguments.

Description

Math.imul() allows for 32-bit integer multiplication with C-like semantics. This feature is useful for projects likeEmscripten.

Becauseimul() is a static method ofMath, you always use it asMath.imul(), rather than as a method of aMath object you created (Math is not a constructor).

If you use normal JavaScript floating point numbers inimul(), you will experience a degrade in performance. This is because of the costly conversion from a floating point to an integer for multiplication, and then converting the multiplied integer back into a floating point. However, withasm.js, which allows JIT-optimizers to more confidently use integers in JavaScript, multiplying two numbers stored internally as integers (which is only possible with asm.js) withimul() could be potentially more performant.

Examples

Using Math.imul()

js
Math.imul(2, 4); // 8Math.imul(-1, 8); // -8Math.imul(-2, -2); // 4Math.imul(0xffffffff, 5); // -5Math.imul(0xfffffffe, 5); // -10

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-math.imul

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp