Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The multiplication operator (*) is a binary arithmetic operator that calculates the product of its two operands. In JavaScript, it operates on both Number (adhering to IEEE 754 double-precision floating-point arithmetic) and BigInt types.
operand1 * operand2

Type Coercion and Evaluation

If an operand is not a primitive Number or BigInt, JavaScript implicitly coerces it using the abstract ToNumeric operation before executing the multiplication. The ToNumeric operation coerces the value to a Numeric type, which can be either a Number or a BigInt, depending on the operand’s primitive representation.
  • Objects: JavaScript attempts to convert the object to a primitive value by calling its valueOf() method, followed by toString(). The resulting primitive is then coerced to a Numeric type. If the object resolves to a BigInt primitive, it evaluates as a BigInt.
  • Booleans: true coerces to 1, false coerces to 0.
  • Strings: Parsed as numeric values. Empty strings ("") coerce to 0. Unparseable strings coerce to NaN.
  • Null/Undefined: null coerces to 0. undefined coerces to NaN.
5 * "4"         // 20 (String coerced to Number)
5 * true        // 5  (Boolean coerced to 1)
5 * null        // 0  (null coerced to 0)
5 * undefined   // NaN (undefined coerced to NaN)
5 * "foo"       // NaN (Unparseable string coerced to NaN)
5 * []          // 0  (Empty array coerces to "", then to 0)
5 * [2]         // 10 (Array coerces to "2", then to 2)
2n * Object(3n) // 6n (Object coerced to BigInt primitive, not Number)

IEEE 754 Floating-Point Precision Loss

Because JavaScript Number types are represented as 64-bit floating-point values, the * operator is subject to floating-point precision loss. Operations involving fractional numbers cannot always be represented exactly in binary, leading to rounding errors during multiplication.
0.1 * 0.2       // 0.020000000000000004

IEEE 754 Edge Cases

The * operator follows specific rules for special numeric values (NaN, Infinity, -Infinity, and -0):
  • NaN: If either operand evaluates to NaN, the result is always NaN.
  • Infinity: Multiplying Infinity by a non-zero number results in Infinity (or -Infinity depending on the sign).
  • Infinity and Zero: Multiplying Infinity (or -Infinity) by 0 (or -0) evaluates to NaN.
  • Signed Zeros: Multiplying two numbers with different signs where the exact mathematical result is zero evaluates to -0.
NaN * 5         // NaN
Infinity * 2    // Infinity
Infinity * -2   // -Infinity
Infinity * 0    // NaN
-1 * 0          // -0

BigInt Operands

The * operator supports BigInt operands, performing arbitrary-precision integer multiplication. However, JavaScript does not allow implicit coercion between Number and BigInt types during arithmetic operations. Both operands must resolve to the same numeric type.
2n * 3n         // 6n
-5n * 4n        // -20n

// Mixing types throws a TypeError
2n * 3          // TypeError: Cannot mix BigInt and other types
Master JavaScript with Deep Grasping Methodology!Learn More