JavaScript | Math.log2() function

The Math.log10() is an inbuilt function in JavaScript which give the value of base 2 logarithms of any number.
Syntax:

Math.log2(p)

    Parameters:

  • Here the parameter p is any number whose base 2 logarithms is to be calculated.
  • Return Values:



  • It returns the value of base 2 logarithms of any number.

Examples:

Input:
Math.log2(5)
Output: 2.321928094887362

Explanation:
Here value of bese 2 logarithms of number 5 is 2.321928094887362 as shown output.

Input:
Math.log2(10)
Output: 3.321928094887362

Let’s see some JavaScript code on this function:
Code #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));

chevron_right


Output:



> 9.965784284662087
> 3.584962500721156
> 4.700439718141092
> 2.321928094887362

Code #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

> 0
> 2
> 2.807354922057604
> 3.321928094887362
> 3.700439718141092
> 4
> 4.247927513443585

Errors and exceptions:

  1. Parameters for this function should always be a number otherwise it return NaN i.e, not a number when its parameter taken as string.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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"));

    chevron_right

    
    

    Output:

    > NaN
    
  2. This function gives error when its parameter taken as complex number because it accept only integer value as the parameter.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    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 needed many times in mathematics problem.
    Let’s see JavaScript code for this application:
    Code #1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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());

    chevron_right

    
    

    Output:

    > 3.807354922057604


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.