Tutorialspoint.dev

JavaScript | Math.PI Property

The Math.PI is a property in JavaScript which is simply used to find the value of Pi i.e, in symbolic form Π which is nothing but it is the ratio of circumference of a circle to its diameter, whose value is approximately 3.141
It is mainly used in mathematics problem.
Difference between property and function in javascript.
Property in JavaScript is nothing but a value whereas method is a function this can be understood anith the help of a example given below.

// car is an object.
var car = {};
  
// car.name is a propety of the given object.
car.name = "Audi",
  
    // car.sayModel is a function of the given object.
    car.sayModel = function() {
        console.log("A8");
    }
  
    // printing property value.
    console.log(car.name);
  
// printing function called value.
console.log(car.sayModel());

Output:

> "Audi"
> "A8"

Here as we can see that property of the object car, is going to store the string as “Audi” and it can be accessed with car.name.
sayModel is a method i.e, a function of the object and it can be accessed with car.sayModel().
It can be noticed that sayModel is just a function which uses ().

Syntax:

Math.PI

    Parameters:



  • Here nothing is passed as a parameter because, Math.PI is not a function but it is a property.
  • Return Values:

  • It simply return the value of PI i.e, Π

Example:

Input:
Math.PI
Output: 3.141592653589793

Explanation:
Here simply value of PI i.e, Π is shown as ouput.

Let’s see JavaScript code for Math.PI property:
Code #1:

// Here value of Math.PI is printed.
console.log(Math.PI);

Output:

> 3.141592653589793

Code #2:
Value of PI i.e, Π can be printed as in the form of function as shown below.

// function is being called.
function get_Value_of_PI()
{
    return Math.PI;
}
  
// function is calling.
console.log(get_Value_of_PI());

Output:

> 3.141592653589793

    Errors and Exceptions:

  • Here we consider Math.PI as a function but in actual it is a property that is why error as output is being shown.
    // Here we consider Math.PI as a function but in actual it
    // is a property that is why error as output is being shown.
    console.log(Math.PI(12));

    Output:

    Error: Math.PI is not a function

Application:
Whenever we need to find the value of anything related to PI that time we take the help of this property.In mathemaatics it needed a lot.
Let’s see JavaScript program on this application:
Code #1:
Here we are going to find the value of area of a circle with given value of its radius.

// function is being called with radius 5 as parameter.
function area_of_circle(radius_of_the_circle)
{
    return Math.PI * radius_of_the_circle * radius_of_the_circle;
}
  
// Here area of the circle is 5 unit.
console.log(area_of_circle(5));

Output:

Here unit is square unit.
> 78.53981633974483


This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter