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 Ninja Code for Beginners

JavaScript Ninja Code points to ways that help a person write code that runs fast and stays easy to read.…

Print in JavaScript Console Using Log, Info, Warn, and Error

The JavaScript console is a tool in web browsers that helps you print the result to the web browser. It…

JavaScript Math asin: How it Works with Examples

Math.asin() finds the arc sine of a number in JavaScript. It helps you work with angles from values. You get…

JavaScript Math ceil: Round Numbers with Precision

Some numbers do not stay whole. You get decimal parts when you divide or calculate prices. You may need to…

JavaScript for of Loop: Syntax and Examples

The for...of loop appeared to solve the problem of complex loops over arrays and strings. It gives you a way…

Bitwise Operators in JavaScript

Bitwise operators work with binary data at the bit level in JavaScript. They process numbers in binary form and return…

Arrow Function in JavaScript: How it Works with Examples

Arrow function gives you short code and keeps this in the right scope in JavaScript. Understand the Arrow Function in…

JavaScript Math abs(): How It Works with Examples

Some numbers come from user input and other come from math operations. Sometimes, these numbers need to stay positive. The…

JavaScript Optional Chaining “?”: How it Works with Examples

JavaScript optional chaining lets you access object values without runtime errors. It checks if a property exists before access and…

Code Structure: How to Structure JavaScript Code

You have to organize your JavaScript code structure. A clean layout helps you read and fix your code. Whether you…

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.