JavaScript Math.log10() Method

Shubham Vora Jan 30, 2023
  1. Syntax of JavaScript Math.log10():
  2. Example Code: Use the Math.log10() Method to Find the Logarithm of Number Base 10
  3. Example Code: Use the Math.log10() Method With Negative Numeric Values
  4. Example Code: Use the Math.log10() Method With Non-Numeric Values
  5. Example Code: Use the Math.log10() Method With the Infinity and 0 Values
JavaScript Math.log10() Method

In JavaScript, we can use the Math.log10() method to find the logarithm of the number when the given base is 10. The Math.log10() method represents the log10(number), which gives output the y such that 10y = number.

Syntax of JavaScript Math.log10():

let output = Math.log10(number);

Parameters

number This is the value at which the base 10 logarithm is calculated.

Return

The Math.log10() method returns the logarithm of the number base 10.

Example Code: Use the Math.log10() Method to Find the Logarithm of Number Base 10

In the example below, we used the Math.log10() method to find the logarithm of a number base 10.

The output shows that the method returns a negative output for all values between 0 and 1. And for values between 1 and 10, this method returns a result between 0 and 1.

The Math.log10() gives the output greater than 1 for all numbers greater than 10.

let output = Math.log10(10);
let output1 = Math.log10(40.32);
let output2 = Math.log10(0.2);
console.log(output);
console.log(output1);
console.log(output2);
console.log(Math.log10(7));

Output:

1
1.605520523437469
-0.6989700043360187
0.8450980400142568

Example Code: Use the Math.log10() Method With Negative Numeric Values

When we use the Math.log10() method with the negative numeric values, it always returns the NaN value as we cannot find the logarithm for the negative values.

let value = Math.log10(-30);
let value1 = Math.log10(-0.32);
let value2 = Math.log10(-45.634);
console.log(value);
console.log(value1);
console.log(value2);

Output:

NaN
NaN
NaN

Example Code: Use the Math.log10() Method With Non-Numeric Values

In this example, we have used the Math.log10() method with non-numeric values such as strings. When we use non-numeric values as the number parameter, it produces the NaN values, which means Not a Number.

let value = Math.log10("Delft");
let value1 = Math.log10("Stack");
console.log(value);
console.log(value1);

Output:

NaN
NaN

Example Code: Use the Math.log10() Method With the Infinity and 0 Values

In the example below, programmers can observe that the Math.log10() method returns the Infinity for the (positive) Infinity value and NaN for the -Infinity value. The method gives the -Infinity output for the 0 value.

let output = Math.log10(Infinity);
let output1 = Math.log10(-Infinity);
console.log(output);
console.log(output1);
console.log(Math.log10(0));
console.log(Math.log10(1));

Output:

Infinity
NaN
-Infinity
0

The Math.log10() method is usable with all modern browsers to find the logarithm of the number given base 10. This article has shown different examples of the Math.log10() method.

Author: Shubham Vora
Shubham Vora avatar Shubham Vora avatar

Shubham is a software developer interested in learning and writing about various technologies. He loves to help people by sharing vast knowledge about modern technologies via different platforms such as the DelftStack.com website.

LinkedIn GitHub

Related Article - JavaScript Math