Math.PI
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Thuộc tính dữ liệu tĩnh Math.PI biểu thị tỷ lệ giữa chu vi và đường kính của một hình tròn, xấp xỉ 3.14159.
Try it
function calculateCircumference(radius) {
return 2 * Math.PI * radius;
}
console.log(Math.PI);
// Expected output: 3.141592653589793
console.log(calculateCircumference(10));
// Expected output: 62.83185307179586
Giá trị
Property attributes of Math.PI | |
|---|---|
| Writable | no |
| Enumerable | no |
| Configurable | no |
Mô tả
Vì PI là thuộc tính tĩnh của Math, bạn luôn sử dụng nó dưới dạng Math.PI, thay vì là thuộc tính của một đối tượng Math bạn tạo ra (Math không phải là constructor).
Ví dụ
>Sử dụng Math.PI
Hàm sau dùng Math.PI để tính chu vi của hình tròn với bán kính truyền vào.
js
function calculateCircumference(radius) {
return Math.PI * (radius + radius);
}
calculateCircumference(1); // 6.283185307179586
Đặc tả kỹ thuật
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-math.pi> |