JavaScript Math log2() Method
Last Updated :
12 Jul, 2024
The Javascript Math.log2() is an inbuilt method in JavaScript that gives the value of base 2 logarithms of any number.
Syntax:
Math.log2(p)
Parameters: This method 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 method:
Example 1: In this example, we will see the logs of various numbers using the Math.log2() method.
JavaScript
// Different numbers are being taken
// as the parameter of the method.
console.log(Math.log2(1000));
console.log(Math.log2(12));
console.log(Math.log2(26));
console.log(Math.log2(5));
Output9.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
// Taken parameter from 1 to 19 incremented by 3.
for (i = 3; i < 20; i += 3) {
console.log(Math.log2(i));
}
Output1.584962500721156
2.584962500721156
3.169925001442312
3.584962500721156
3.9068905956085187
4.169925001442312
Errors and exceptions: Parameters for this method 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() method returns Nan as a string is passed in the method.
JavaScript
// Parameters for this method 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"));
Example 2: This method gives an error when its parameter is taken as a complex number because it accepts only integer value as the parameter.
JavaScript
// Parameters can never be a complex number because
// it accept only integer value as the parameter.
console.log(Math.log2(1 + 2i));
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 method. 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
// taking parameter as number 14 and
//calculated in the form of method.
function value_of_base_2_logarithms_of_any_number() {
return Math.log2(14);
}
console.log(value_of_base_2_logarithms_of_any_number());
We have a complete list of Javascript Math methods, to check those please go through this JavaScript Math Object Complete Reference article.
Supported Browsers:
- Google Chrome 38 and above
- Edge 12 and above
- Firefox 25 and above
- Opera 25 and above
- Safari 8 and above
Similar Reads
JavaScript Math E Property The Math.E is a property in JavaScript that simply returns the value of Euler's Number, which is nothing but just has a base of the natural logarithm. Its value is approximately around of 2.71828.Syntax:Math.E;Math.E â 2.718Return Values:It simply returns the value of the base of the natural logarit
2 min read
JavaScript Math LN2 Property The Math.LN2 is a property in JavaScript that is simply used to find the value of a natural log of 2. The natural log is of base e which is represented as ln. So, the natural log of 2 is represented as ln(2) whose value is approximately 0.693.Syntax:Math.LN2; Parameters: This method does not accept
2 min read
JavaScript Math LN10 Property The Math.LN10 is a property in JavaScript that is simply used to find the value of a natural log of 10. The natural log is of base e which is represented as ln. So, the natural log of 10 is represented as ln(10) whose value is approximately 2.302Syntax:Math.LN10; Return Values: It simply returns the
2 min read
JavaScript Math LOG2E Property The Javascript Math.LOG2E is a property in JavaScript that is simply used to find the value of base 2 logarithms of e, where e is an irrational and transcendental number approximately equal to 1.442.Syntax:Math.LOG2E;Return Values: It simply returns the value of the base 2 logarithms of e.Example: H
3 min read
JavaScript Math LOG10E Property The Javascript Math.LOG10E is a property in JavaScript that is simply used to find the value of base 10 logarithms of e, where e is an irrational and transcendental number approximately equal to 0.434Syntax: Math.LOG10E; Return Values: It simply returns the value of the base 10 logarithms of e.Examp
3 min read
JavaScript Math PI Property The Math.PI is a property in JavaScript that is simply used to find the value of Pi i.e, in symbolic form Î which is nothing but it is the ratio of the circumference of a circle to its diameter, whose value is approximately 3.141. It is mainly used in mathematics problems.Syntax:Math.PIReturn values
3 min read
JavaScript Math SQRT1_2 Property The Javascript Math.SQRT1_2 is a property in JavaScript that is simply used to find the value of the square root of 1/2, whose value is approximately 0.707106. That is, â (1/2) = 0.707106Syntax: Math.SQRT1_2;Return Values: It simply returns the value of the square root of 1/2, whose value is approxi
2 min read
JavaScript Math SQRT2 Property The Math.SQRT2 is a property in JavaScript which is simply used to find the value of the square root of 2, whose value is approximately 1.4142. That is, â 2 = 1.4142 Syntax: Math.SQRT2; Return Value: It simply returns the value of the square root of 2, whose value is approximately 1.4142. Below is a
2 min read
JavaScript Math abs() Method Javascript Math.abs() method is used to return the absolute value of a number. It takes a number as its parameter and returns its absolute value. Syntax:Math.abs(value)Parameters:This method accepts a single parameter as mentioned above and described below:value: The number whose absolute value is t
2 min read
JavaScript Math acos() Method The Javascript Math.acos( ) method is used to return the arccosine of a number in radians. The Math.acos() method returns a numeric value between 0 and pi radians. The Math acos() is a static method of Math, therefore, it is always used as Math.acos(), rather than as a method of a Math object create
2 min read