Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript
math
log
communitycreator

# What is Math.log2() in JavaScript? Arslan Bajwa

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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 type Number, for which we want to find log2().

Number in JavaScript is a 64-bit double-precision value that is the same as double in C# or Java.

## Return value

• Number: It returns the base 2 logarithm of the input parameter param. It is of Number type.

• -Infinity: The function returns this if the input parameter param = 0.

• +Infinity: The function returns this if the input parameter param = Infinity.

• NaN: The function returns this if the input parameter param < 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

RELATED TAGS

javascript
math
log
communitycreator

CONTRIBUTOR Arslan Bajwa

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring

Learn in-demand tech skills in half the time 