Using Math log2() function to coding in Javascript/JS

This JavaScript tutorial explains how to use the math characteristic referred to as log2() with syntax and examples.


In JavaScript, log2() is a characteristic that is used to return the base-2 logarithm of a number. Because the log2() feature is a static characteristic of the Math object, it should be invoked through the placeholder object referred to as Math.


In JavaScript, the syntax for the log2() characteristic is:


Parameters or Arguments

number The variety to use in the calculation. It have to be a value that is greater than 0


The log2() function returns the base-2 logarithm of a number.

If the number is 0, the log2() feature will return -Infinity.

If the number is a negative value, the log2() function will return NaN.


Math is a placeholder object that incorporates mathematical functions and constants of which log2() is one of these functions.


Let’s take a seem to be at an instance of how to use the log2() feature in JavaScript.

For example:


In this example, we have invoked the log2() characteristic the usage of the Math class.

We have written the output of the log2() feature to the net browser console log, for demonstration purposes, to show what the log2() characteristic returns.

The following will be output to the web browser console log:


In this example, the first output to the console log back zero which is the base-2 logarithm of 1.

The 2d output to the console log again 1.3219280948873624 which is the base-2 logarithm of 2.5.

The second output to the console log back -Infinity in view that the quantity furnished was once zero

The second output to the console log back NaN due to the fact the variety supplied was once a negative value.