It is equivalent to log2(x) in mathematics.
The syntax of the Math.log2()
function is:
Math.log2(x)
log2()
, being a static method, is called using the Math
class name.
Math.log2() Parameters
The Math.log2()
function takes in:
- x - A number
Return value from Math.log2()
- Returns the base 2 logarithm of the given number.
- Returns
NaN
for negative numbers and non-numeric arguments.
Example: Using Math.log2()
// Using Math.log2()
var value = Math.log2(1);
console.log(value); // 0
var value = Math.log2(2);
console.log(value); // 1
var value = Math.log2("10");
console.log(value); // 3.321928094887362
var value = Math.log2(0);
console.log(value); // -Infinity
var value = Math.log2(-1);
console.log(value); // NaN
Output
0 1 3.321928094887362 -Infinity NaN
Notes:
- Use the constant
Math.LOG2E
for log2(e). - Use the functions
Math.log()
orMath.log10()
for logarithm base e and 10.
Recommended readings: