Math.log( ) In JavaScript
The Math.log() function in JavaScript is used to return the natural logarithm (base e) of a number. The JavaScript Math.log() function is equivalent to ln(x) in mathematics.
If the value of x is negative, then math.log() function return NaN.
log() is a static method of Math, therefore, it is always used as Math.log(), rather than as a method of a Math object created.
Syntax:
Math.log(value)
Parameters: this function accepts a single parameter value which is a number whose natural logarithm you want to find.
Returns: The Math.log() function returns the natural logarithm given number.
Examples:
- When zero is passed as a parameter.
<
script
type
=
"text/javascript"
>
document.write("Output : " + Math.log(0));
</
script
>
chevron_rightfilter_noneOutput:
Output : -Infinity
- When “-1” is passed as a parameter.
<
script
type
=
"text/javascript"
>
document.write("Output : " + Math.log(-1));
</
script
>
chevron_rightfilter_noneOutput:
Output : NaN
- When “10” is passed as a parameter.
<
script
type
=
"text/javascript"
>
document.write("Output : " + Math.log(10));
</
script
>
chevron_rightfilter_noneOutput:
Output : 2.302585092994046
- Calculating Math.log() with a different base.
For finding the logarithm of 8 with base 2,execute the math.log() function in the following way:<
script
type
=
"text/javascript"
>
document.write("Output : " + Math.log(8)/Math.log(2));
</
script
>
chevron_rightfilter_noneOutput:
Output : 3
- Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari
- Introduction to JavaScript Course | Learn how to Build a task tracker using JavaScript
- How to compare two JavaScript array objects using jQuery/JavaScript ?
- JavaScript Course | Understanding Code Structure in JavaScript
- JavaScript Course | Conditional Operator in JavaScript
- JavaScript Course | Printing Hello World in JavaScript
- JavaScript Course | Data Types in JavaScript
- JavaScript Course | Logical Operators in JavaScript
- JavaScript Course | Loops in JavaScript
- JavaScript Course | Functions in JavaScript
- JavaScript Course | Operators in JavaScript
- JavaScript Course | Variables in JavaScript
- JavaScript Course | Objects in JavaScript
- JavaScript Course | JavaScript Prompt Example
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- How to include a JavaScript file in another JavaScript file ?
Supported Browsers: The browsers supported by JavaScript Math.log( ) function are listed below:
Recommended Posts:
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.