CKTIS / JavaScript
Math.log2 — base-2 logarithm
Math.log2(x) calculates the base-2 logarithm: the exponent to which 2 must be raised to obtain x. Learn about argument conversion, special results, and precision limits.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
Math.log2(x)Math.log2(x) returns the base-2 logarithm as a Number. Mathematically, the result corresponds to the exponent in 2 ** y = x. Call the method directly on Math.
The argument first undergoes ToNumber conversion. This is not type validation: values of other types may be converted, and some conversions throw an exception.
For 1, the result is +0; for both +0 and -0, it is -Infinity. Negative values, including -Infinity, produce NaN. An argument of NaN produces NaN, and Infinity produces Infinity.
For other positive finite values, the specification permits an implementation-dependent approximation. Do not assume perfect precision or use the result alone as an exact power-of-two test. This is an ECMAScript language API, not a Web API; the specification alone does not establish support in a particular browser.
Examples
const inputs = [1, 0, -0, -1, Infinity, NaN];
console.log(inputs.map(x => String(Math.log2(x))).join('\n'));Result
0 -Infinity -Infinity NaN Infinity NaN
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.