C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
JavaScript Math log() methodThe JavaScript math log() method returns the natural logarithm of the given number i.e. base e. If the number is negative, it returns NaN. SyntaxThe log() method is represented by the following syntax: Math.log(num) Parameternum - A number. ReturnThe natural logarithm of a number. JavaScript Math log() method exampleHere, we will understand log() method through various examples. Example 1Let's see an example to print the natural logarithm of a number. <script> document.writeln(Math.log(1)+"<br>"); document.writeln(Math.log(5)+"<br>"); document.writeln(Math.log(10)); </script> Output: 0 1.6094379124341003 2.302585092994046 Example 2Let's see the result of log() method in different test cases. <script> document.writeln(Math.log()+"<br>"); document.writeln(Math.log(-5)+"<br>"); document.writeln(Math.log(0)); </script> Output: NaN NaN -Infinity Example 3Here, you can test log() method with your own test cases. <script> function display() { var x=document.getElementById("num").value; document.getElementById("result").innerHTML=Math.log(x); } </script> <form> Enter a number: <input type="text" id="num"> <input type="button" onclick="display()" value="submit"> </form> <p><span id="result"></span></p>
Next TopicJavaScript Math
|