What is Math.log2() in JavaScript?
Math is a built-in object in JavaScript that contains different methods and properties used to perform mathematical operations. It contains a log2() function, which is used to compute the base 2 logarithm of a specified number.
Syntax
Math.log2(param);
Parameter
param: This is the input value of the typeNumber, for which we want to findlog2().
Numberin JavaScript is a 64-bit double-precision value that is the same asdoublein C# or Java.
Return value
-
Number: It returns the base 2 logarithm of the input parameterparam. It is ofNumbertype. -
-Infinity: The function returns this if the input parameterparam= 0. -
+Infinity: The function returns this if the input parameterparam=Infinity. -
NaN: The function returns this if the input parameterparam< 0.
Example
console.log("log2(-1) = " + Math.log2(-1));console.log("log2(0) = " + Math.log2(0));console.log("log2(1) = " + Math.log2(1));console.log("log2(20) = " + Math.log2(20));console.log("log2(∞) = " + Math.log2(Infinity));console.log("log2(-∞) = " + Math.log2(-Infinity));
Output
- log2 ( -1 ) = NaN
- log2 ( 0 ) = -Infinity
- log2 ( 1 ) = 0
- log2 ( 20 ) = 4.321928094887363
- log2 ( ∞ ) = Infinity
- log2 ( -∞ ) = NaN