Skip to content
Related Articles
Open in App
Not now

Related Articles

JavaScript Math.log2() Function

Improve Article
Save Article
  • Last Updated : 26 Dec, 2022
Improve Article
Save Article

The Javascript Math.log2() is an inbuilt function in JavaScript that gives the value of base 2 logarithms of any number. 

Syntax: 

Math.log2(p)

Parameters: This function accepts a single parameter p which is any number whose base 2 logarithms is to be calculated.

Returns: It returns the value of base 2 logarithms of any number.

Let’s see some JavaScript code on this function: 

Example 1: In this example, we will see the logs of various numbers using the Math.log2() function.

Javascript




<script>
    // Different numbers are being taken
    // as the parameter of the function.
    console.log(Math.log2(1000));
    console.log(Math.log2(12));
    console.log(Math.log2(26));
    console.log(Math.log2(5));
</script>

Output: 

9.965784284662087
3.584962500721156
4.700439718141092
2.321928094887362

Example 2: In this example, we will use a for loop to get the log of numbers that are a multiple of three upto 20.

Javascript




<script>
    // Taken parameter from 1 to 19 incremented by 3.
    for (i = 3; i < 20; i += 3) {
        console.log(Math.log2(i));
    }
</script>

Output: 

1.584962500721156
2.584962500721156
3.169925001442312
3.584962500721156
3.9068905956085187
4.169925001442312

Errors and exceptions: Parameters for this function should always be a number otherwise it returns NaN i.e, not a number when its parameter is taken as a string.  

Example 1: In this example, the Math.log2() function returns Nan as a string is passed in the function.

Javascript




<script>
    // Parameters for this function should always be a
    // number otherwise it return NaN i.e, not a number
    // when its parameter taken as string.
    console.log(Math.log2("gfg"));
</script>

Output: 

NaN

Example 2: This function gives an error when its parameter is taken as a complex number because it accepts only integer value as the parameter.

Javascript




<script>
    // Parameters can never be a complex number because
    // it accept only integer value as the parameter.
    console.log(Math.log2(1 + 2i));
</script>

Output: 

Error: Invalid or unexpected token

Application: Whenever we need the value of base 2 logarithms of any number that time we take the help of this function. Its value is needed many times in mathematics problems. 

Let’s see the JavaScript code for this application: 

Example 1: This example shows the demonstration of the above approach.

Javascript




<script>
    // taking parameter as number 14 and
    //calculated in the form of function.
    function value_of_base_2_logarithms_of_any_number()
    {
      return Math.log2(14);
    }
       console.log(value_of_base_2_logarithms_of_any_number());
</script>

Output: 

3.807354922057604

We have a complete list of Javascript Math methods, to check those please go through this JavaScript Math Object Complete Reference article.

Supported Browsers: The browsers supported by JavaScript Math.log2() function are listed below:  

  • Google Chrome 38 and above
  • Edge 12 and above
  • Firefox 25  and above
  • Opera 25 and above
  • Safari 8 and above

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!