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.
Example 1: In this example, we will see the logs of various numbers using the Math.log2() function.
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.
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.
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.
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.
Example 1: This example shows the demonstration of the above approach.
- Google Chrome 38 and above
- Edge 12 and above
- Firefox 25 and above
- Opera 25 and above
- Safari 8 and above