Use Math.PI To Calculate Area And Circumference Of A Circle
Introduction
The Math.PI
property returns the PI π
constant, approximately equal to 3.14159. For example, you can calculate the circumference and surface area of a circle using Math.PI
.
console.log(Math.PI); // 3.141592653589793
Calculating Circumference Of A Circle
Write a function that accepts a number
as the circle's radius as the following example:
function circleCircumference(radius) { return 2 * Math.PI * radius; } console.log(circleCircumference(16)); // 100.53096491487338
Calculating Area Of A Circle
Like the previous example, write a function to calculate the surface area:
function circleArea(radius) { return Math.PI * (radius * radius); } console.log(circleArea(16)); // 804.247719318987
You can re-write the above function by using the Math.pow()
to calculate the radius^2
:
function circleArea(radius) { return Math.PI * Math.pow(radius, 2); } console.log(circleArea(16)); // 804.247719318987
Also read about the Math.ceil() function and Math.floor() function.