Get to know MDN better
Esta página ha sido traducida del inglés por la comunidad. Aprende más y únete a la comunidad de MDN Web Docs.
This feature is well established and works across many devices and browser versions. It’s been available across browsers since julio de 2015.
La función Math.log2() retorna el logaritmo base 2 de un número, esto es
∀x>0,Math.log2(x)=log2(x)=the uniqueysuch that2y=x\forall x > 0, \mathtt{\operatorname{Math.log2}(x)} = \log_2(x) = \text{the unique} ; y ; \text{such that} ; 2^y = xUn número.
El logaritmo base 2 del número usado como parámetro. Si el número es negativo, NaN será retornado.
Si el valor de x es mejor a 0, el valor de retorno es siempre NaN.
Debido a que log2() es una función estática de Math, siempre debe ser usado como Math.log2(), en lugar de ser usado como un método del objeto Math (Math no es un constructor).
Esta función es equivalente a Math.log(x) / Math.log(2). Para log2(e) use la constante Math.LOG2E que es 1 / Math.LN2.
This Polyfill emulates the Math.log2 function. Note that it returns imprecise values on some inputs (like 1 << 29), wrap into Math.round() if working with bit masks.
| ECMAScript® 2027 Language Specification # sec-math.log2 |
Enable JavaScript to view this browser compatibility table.
This page was last modified on 11 feb 2025 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.