JS: Math exponential and logarithm

By Xah Lee. Date: . Last updated: .
Math.exp(x)

e raised to the power of x.

console.log(Math.exp(1));
// 2.718281828459045

console.log(Math.exp(1) === Math.E);
// true

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

(new in ECMAScript 2015)

Same as Math.exp(x) -1 but the result is computed in a way that is accurate even when the value of x is close 0.

console.log(Math.expm1(0.01));
// 0.010050167084168058

console.log(Math.exp(0.01) - 1);
// 0.010050167084167949

console.log(Math.pow(Math.E, 0.01) - 1);
// 0.010050167084167949
Math.log(x)

Natural log of x

console.log(Math.log(Math.E));
// 1

console.log(Math.E);
// 2.718281828459045

console.log(Math.log(2.718281828459045));
// 1

console.log(Math.log(2));
// 0.6931471805599453
Math.log10(x)

(new in ECMAScript 2015)

Base 10 log of x

console.log(Math.log10(100));
// 2

console.log(Math.log10(1000));
// 3

console.log(Math.log10(10000));
// 4
Math.log1p(x)

(new in ECMAScript 2015)

Natural log of (x+1)

console.log(Math.log1p(Math.E) === Math.log(Math.E + 1));
// true

console.log(Math.log1p(Math.E));
// 1.3132616875182228

console.log(Math.log1p(Math.E -1));
// 1
Math.log2(x)

(new in ECMAScript 2015)

Base 2 log of x

console.log(Math.log2(2));
// 1

console.log(Math.log2(4));
// 2

console.log(Math.log2(8));
// 3

console.log(Math.log2(16));
// 4

JavaScript. math functions.