In JavaScript, exp() is a function that is used to return e raised to the power of number (which is enumber). Because the exp() function is a static function of the Math object, it must be invoked through the placeholder object called Math.
Math.exp(x)
x
− A numbers.Returns the exponential value of the variable x.
Note
console.log(Math.exp(0));
console.log(Math.exp(1));
console.log(Math.exp(-2));
In this example, we have invoked the exp() function using the Math class.
We have written the output of the exp() function to the web browser console log, for demonstration purposes, to show what the exp() function returns.
Output
1
2.718281828459045
0.1353352832366127
In this example, the first output to the console log returned 1 which is the calculation of e0.
The second output to the console log returned 2.718281828459045 which is the calculation of e1.
The third output to the console log returned 0.1353352832366127 which is the calculation of e-2.
The following example demonstrates that if the passed value is not a valid number, then the method returns NaN.
var a = "JavaScript";
var b;
console.log(Math.exp(a));
console.log(Math.exp(b));
Output
NaN
NaN
The following example demonstrates the case of the parameter as infinity(positive/negative) and zero(positive/negative).
For example:
var a = Number.POSITIVE_INFINITY;
var b = Number.NEGATIVE_INFINITY;
var c = 0;
var d = -0;
console.log(Math.exp(a));
console.log(Math.exp(b));
console.log(Math.exp(c));
console.log(Math.exp(d));
Output
Infinity
0
1
1
The following example demonstrates that if null is passed as an argument, then this method returns 1.
var a = null;
var b = "";
var c = [];
console.log(Math.exp(a));
console.log(Math.exp(b));
console.log(Math.exp(c));
Output
1
1
1
The Javascript exp() method cannot be used with complex arguments as only integer arguments are accepted.
//Complex values cannot be passed as arguments as follows
//since only integer arguments are accepted.
console.log(Math.exp(2+i));
Output
ReferenceError: i is not defined
Thanks for reading !
#js #javascript