How to Returns PI (approx. 3.14) in JavaScript ?


There are different ways to return PI (approx. 3.14) in JavaScript:

  • Using the Math.PI property: The Math.PI property returns the value of PI (approx. 3.14).

Example:

console.log(Math.PI); // Output: 3.141592653589793
  • Using the Number.prototype.toFixed() method: The toFixed() method returns a string representation of a number with a fixed number of decimal points.

Example:

console.log(Math.PI.toFixed(2)); // Output: 3.14
  • Using the parseFloat() function: The parseFloat() function parses a string argument and returns a floating point number.

Example:

console.log(parseFloat("3.14")); // Output: 3.14


About the author

William Pham is the Admin and primary author of Howto-Code.com. With over 10 years of experience in programming. William Pham is fluent in several programming languages, including Python, PHP, JavaScript, Java, C++.