Tutorialspoint.dev

JavaScript | Math.log10() function

The Math.log10() is an inbuilt function in JavaScript which give the value of base 10 logarithms of any number.
Syntax:

Math.log10(p)

    Parameters:

  • Here the parameter p is any number whose base 10 logarithms is to be calculated.
  • Return Values:



  • It returns the value of base 10 logarithms of any number.
  • Examples:

    Input:
    Math.log10(5)
    Output: 0.6989700043360189

    Explanation:
    Here value of base 10 logarithms of number 5 is 0.6989700043360189 as shown output.

    Input:
    Math.log10(10)
    Output: 1

Let’s see some JavaScript code on this function:

// Different numbers are being taken
// as the parameter of the function.
console.log(Math.log10(1000));
console.log(Math.log10(12));
console.log(Math.log10(26));
console.log(Math.log10(5));

Output:



> 3
> 1.0791812460476249
> 1.414973347970818
> 0.6989700043360189

Code #2:

// Taken parameter from 1 to 19 incremented by 3.
for (i = 1; i < 20; i += 3) {
    console.log(Math.log10(i));
}

Output:

> 0
> 0.6020599913279624
> 0.8450980400142568
> 1
> 1.1139433523068367
> 1.2041199826559248
> 1.2787536009528289

    Errors and exceptions:

  1. Parameters for this function should always be a number otherwise it return NaN i.e, not a number when its parameter taken as string.

    // Parameters for this function should always be a
    // number otherwise it return NaN i.e, not a number
    // when its parameter taken as string.
    console.log(Math.log10("gfg"));

    Output:

    > NaN
    
  2. This function gives error when its parameter taken as complex number because it accept only integer value as the parameter.
    // Parametes can never be a complex number because
    // it accept only integer value as the parameter.
    console.log(Math.log10(1 + 2i));

    Output:

    Error: Invalid or unexpected token
    

    Application:

  • Whenever we need the value of base 10 logarithms of any number that time we take the help of this function.Its value needed many times in mathematics problem.
    Let’s see JavaScript code for this application:
    Code #1:

    // taking parameter as number 14 and calculated in the form of function.
    function value_of_base_10_logarithms_of_any_number()
    {
        return Math.log10(14);
    }
    console.log(value_of_base_10_logarithms_of_any_number());

    Output:

    > 1.146128035678238


This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter