Math.log2()
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.
Die statische Methode Math.log2()
gibt den Logarithmus zur Basis 2 einer Zahl zurück. Das bedeutet:
Probieren Sie es aus
console.log(Math.log2(3));
// Expected output: 1.584962500721156
console.log(Math.log2(2));
// Expected output: 1
console.log(Math.log2(1));
// Expected output: 0
console.log(Math.log2(0));
// Expected output: -Infinity
Syntax
Math.log2(x)
Parameter
x
-
Eine Zahl, die größer oder gleich 0 ist.
Rückgabewert
Der Logarithmus zur Basis 2 von x
. Falls x < 0
, wird NaN
zurückgegeben.
Beschreibung
Da log2()
eine statische Methode von Math
ist, wird sie immer als Math.log2()
verwendet und nicht als Methode eines erstellten Math
-Objekts (Math
ist kein Konstruktor).
Diese Funktion entspricht Math.log(x) / Math.log(2)
. Für log2(e)
verwenden Sie die Konstante Math.LOG2E
, welche 1 / Math.LN2
ist.
Beispiele
Verwendung von Math.log2()
Math.log2(-2); // NaN
Math.log2(-0); // -Infinity
Math.log2(0); // -Infinity
Math.log2(1); // 0
Math.log2(2); // 1
Math.log2(3); // 1.584962500721156
Math.log2(1024); // 10
Math.log2(Infinity); // Infinity
Spezifikationen
Specification |
---|
ECMAScript® 2025 Language Specification # sec-math.log2 |
Browser-Kompatibilität
BCD tables only load in the browser