Javascript log: How to Find Natural Log in JavaScript
The math.log() method can come in handy in several programs involving mathematical calculations.
Javascript log
JavaScript log(x) is a built-in math library method used to find the natural logarithm of any positive x. For example, a log of x to the base e, where e is Euler’s number. The Math.log() function is equivalent to ln(x) in mathematics. The log() is a static method of Math, and it can be used without creating an object.
In JavaScript, for the natural logarithm of 2 and 10, constants Math.LN2 and Math.LN10 exist.
Syntax
Math.log(x)
Parameter(s)
The variable x, whose natural logarithm is to be determined.
Return value
The natural logarithm of x.
See the following figure.
Note:
- For a positive argument x, Math.log(Math.exp(x)) gives value x.
- If a negative argument is passed, the method returns NaN.
- If the argument is negative infinity, then this method returns NaN.
- If the argument is positive infinity, then this method returns positive infinity.
- If the argument is positive zero or negative zero, the log() method returns negative infinity.
- If the passed value is not a valid number, the method returns NaN.
- If the passed value is null, the method returns negative infinity.
- Base Change Theorem : logax / logay = logyx
Compatibility(Version and above)
- Google Chrome v1
- Internet Explorer v3
- Firefox v1
- Edge v12
- Opera
- Safari v1
- Android webview v1
- Chrome for Android v18
- Firefox for Android v4
- Opera for Android
- Safari on iOS v1
- Samsung Internet v1.0
- Node.js
JavaScript version: ECMAScript 1
Javascript log
Consider the following examples.
The following example demonstrates the js log() method.
// example1.js var a = 1; var b = 2; var c = 3; var d = 4; console.log(Math.log(a)); console.log(Math.log(b)); console.log(Math.log(c)); console.log(Math.log(d));
Output
node example1 0 0.6931471805599453 1.0986122886681096 1.3862943611198906
Example 2
The following code example demonstrates that if the passed value is not a valid number, then the method returns NaN.
// example2.js var a = "JavaScript"; var b; console.log(Math.log(a)); console.log(Math.log(b));
Output
node example2 NaN NaN
Math.log() with complex arguments (Example 3)
The Math.log() method cannot be used with complex arguments as only integer arguments are accepted.
See the following code.
// example3.js console.log(Math.log(2+i));
Output
ReferenceError: i is not defined
Example 4
The following example demonstrates that for a positive x: ln(ex) = x.
// example4.js var x = 10; var a = Math.exp(x); console.log(Math.log(a));
Output
node example4 10
Pass infinity in Javascript log() argument (Example 5)
The following example demonstrates the case of a parameter as infinity(positive/negative) and zero(positive/negative).
See the following code.
// example5.js var a = Number.POSITIVE_INFINITY; var b = Number.NEGATIVE_INFINITY; var c = 0; var d = -0; console.log(Math.log(a)); console.log(Math.log(b)); console.log(Math.log(c)); console.log(Math.log(d));
Output
node example5 Infinity NaN -Infinity -Infinity
Pass null in Javascript log() as an argument (Example 6)
The following example demonstrates that if null is passed as an argument, this method returns negative infinity.
// example6.js var a = null; var b = ""; var c = []; console.log(Math.log(a)); console.log(Math.log(b)); console.log(Math.log(c));
Output
node example6 Infinity Infinity Infinity
Example 7
The following example demonstrates that if negative parameters are passed, this method returns NaN.
// example7.js var a = -1; var b = -2; var c = Number.NEGATIVE_INFINITY; console.log(Math.log(a)); console.log(Math.log(b)); console.log(Math.log(c));
Output
node example7 NaN NaN NaN
Example 8
The following example demonstrates an application of this method in a simple programming context.
Compute the logarithm of a given argument to any given base.
[In this example, we will apply the base change theorem of the logarithm, which states that:
logax / logay = logyx ]
// example8.js var arg; var base; const r = require('readline'); const rl = r.createInterface({ input: process.stdin, output: process.stdout }); const prompt1 = () => { return new Promise((resolve, reject) => { rl.question('Argument: ', (answer) => { arg = answer; resolve(); }); }); }; const prompt2 = () => { return new Promise((resolve, reject) => { rl.question('Base: ', (answer) => { base = answer; resolve(); }); }); }; const main = async () => { await prompt1(); await prompt2(); rl.close(); var result = Math.log(arg) / Math.log(base); // Base Change Theorem result = Math.round(result); console.log("Log of " + arg + " to the base " + base + " is: " + result); } main();
Output
node example8 Test Case 1: Argument: 8 Base: 2 Log of 8 to the base 2 is: 3 Test Case 2: Argument: 1000 Base: 10 Log of 1000 to the base 10 is: 3
Example 9
The following example demonstrates the constants Math.LN2 and Math.LN10.
// example9.js var a = Math.LN2; var b = Math.LN10; var c = Math.log(2); var d = Math.log(10); console.log(a); console.log(b); console.log(); console.log(c); console.log(d);
Output
node example9 0.6931471805599453 2.302585092994046 0.6931471805599453 2.302585092994046