JavaScript math.exp: How to Calculate e^x

javascript math exp

Math.exp is a built-in JavaScript function that returns the value of e raised to a given number.

Here, e is a constant that has a value of about 2.718. This function is part of the Math object, which means you call it as Math.exp(). You cannot use it as a method on numbers.

You can use Math.exp when you work with formulas that involve growth, decay, or other calculations based on the exponential number e.

How the math.exp Works in JavaScript

Math.exp(x) returns e to the power of x. The x can be any real number, positive, negative, or zero.

Here is the basic syntax:

Math.exp(x)

x — A number that acts as the exponent.

The result is always a number. If x is NaN, the function returns NaN. If x is positive infinity, the result is positive infinity, and if x is negative infinity, the result is zero.

The function multiplies e by itself x times in a mathematical sense. In reality, it uses an internal algorithm that works for any number, even fractions or negatives. For example, Math.exp(1) returns the constant e, and Math.exp(0) returns 1.

Here is an example:

console.log(Math.exp(2)); // 7.38905609893065

Here, the function returns e squared. Since e is about 2.718, squaring it gives about 7.389.

The Math.exp works in all major browsers, including Chrome, Firefox, Safari, Edge, and Opera. It is also available in Node.js, so it can be used in both browser and server environments.

Combine math.exp with Other Math Functions in JavaScript

Math.exp(x) returns e (Euler’s number, about 2.718) raised to the power of x. It becomes more useful when combined with other Math functions to create formulas.

For example, you can use it with Math.log() to work with exponential and logarithmic equations:

console.log(Math.exp(Math.log(20))); // 19.999999999999996

It can also work with Math.pow() for more complex models. A common use is in finance for compound interest:

// Continuous compound interest formula: A = P * e^(r * t)
let P = 1000; // principal
let r = 0.05; // interest rate
let t = 3;    // years
let A = P * Math.exp(r * t);
console.log(A.toFixed(2)); // 1161.83

Examples

Calculate Continuous Growth:

let rate = 0.05;
let years = 10;
let final = 1000 * Math.exp(rate * years);
console.log(final); // 1648.721270700128

This code uses Math.exp to find the final value after continuous growth at a rate of 5% per year for 10 years. The number grows faster than with normal interest formulas.

Use Math.exp with Negative Numbers:

let decayRate = -0.1;
let time = 5;
let result = Math.exp(decayRate * time);
console.log(result); // 0.6065306597126334

Here, the result shows exponential decay. Since the exponent is negative, the value moves toward zero over time instead of growing.

Link Math.exp with Math.log:

let number = 50;
let logValue = Math.log(number);
let original = Math.exp(logValue);
console.log(original); // 49.99999999999999

This example proves that Math.exp can reverse Math.log. The value returned by the log function is the power that Math.exp needs to get the original number.

Calculate e to a Fractional Power:

let fraction = 0.5;
let result = Math.exp(fraction);
console.log(result); // 1.6487212707001282

When you pass a fraction to Math.exp, the result is the square root of e. This works the same way as fractional exponents in other math operations.

Wrapping Up

In this article, you learned what Math.exp is and how it works in JavaScript. You also saw how it combines with other Math functions and how to use it in different contexts.

Here is a quick recap:

  • Math.exp(x) returns e to the power of x.
  • The result is always a number and works for positive, negative, and fractional values.
  • It can be combined with other math functions for advanced formulas.
  • It is supported by all major browsers and Node.js.

FAQs

How does JavaScript math.exp work?

Explanation: The Math.exp(x) function returns e raised to the power of x. Here, e is Euler's number (~2.718). Example:
let result = Math.exp(2);
console.log(result); // 7.38905609893065

What is the difference between Math.exp and Math.pow in JavaScript?

Math.exp(x) calculates e^x. Math.pow(base, exponent) calculates base^exponent. Example:
console.log(Math.exp(2));       // 7.38905609893065
console.log(Math.pow(2, 3));    // 8

Can Math.exp handle negative numbers in JavaScript?

Yes, Math.exp can handle negative numbers. Example:
console.log(Math.exp(-1)); // 0.36787944117144233
console.log(Math.exp(0));  // 1

How to use Math.exp in calculations?

Math.exp is useful in finance, science, and probability calculations. Example:
  • Compound interest: A = P * Math.exp(r * t)
  • Exponential growth: population = initial * Math.exp(rate * time)

Similar Reads

JavaScript Syntax: The Complete Cheat Sheet

JavaScript syntax is what you will use when writing and reading javascript code, it is the foundation of using this…

JavaScript Math sqrt: How to Find Square Root of a Number

JavaScript Math.sqrt() solves the need to get square roots fast. Before it, you wrote custom code or used loops. It…

JavaScript Class and Object Constructors with Examples

Object constructors and class constructors in JavaScript create objects in a structured way. Both provide methods to build reusable code.…

JavaScript Math atan: Arc Tangent of a Number in Radians

Math.atan() function in JavaScript helps you to solve math problems in your code. It turns a number into an angle.…

JavaScript Symbol Type: How it Works with Examples

JavaScript Symbol Type gives a way to make unique values. A Symbol never matches any other value and this helps…

JavaScript Loops and Iteration: How They Work with Examples

You use loops in JavaScript to repeat code without copying it many times. This helps when you handle lots of…

JavaScript Nullish Coalescing Operator Guide with Examples

The JavaScript nullish coalescing operator (??) gives a default value when a variable is null or undefined. Understand the Nullish…

JavaScript toSpliced Function Explained with Examples

The toSpliced function creates a new array without changing the original array in JavaScript. It returns a copy with new…

JavaScript toString Function: How it Works with Examples

The toString function in JavaScript converts a value to a string form without change. It works on numbers, arrays, and…

forEach Loop in JavaScript: A Complete Guide with Examples

The forEach loop in JavaScript allows you to go through each item in an array. You do not have to…

Previous Article

HTML Introduction for Beginners from Scratch

Next Article

PHP array_intersect_key Function: How it Works with Examples

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.