Math.PI
Math.PI 静态数据属性表示一个圆的周长与直径的比率,近似值为 3.14159。
试一试
function calculateCircumference(radius) {
return 2 * Math.PI * radius;
}
console.log(Math.PI);
// Expected output: 3.141592653589793
console.log(calculateCircumference(10));
// Expected output: 62.83185307179586
值
Math.PI 属性特性 | |
|---|---|
| 可写 | 否 |
| 可枚举 | 否 |
| 可配置 | 否 |
描述
因为 PI 是 Math 的静态属性,所以你总是通过 Math.PI 来使用它,而不是通过你自己创建的 Math 对象属性来使用(Math 不是一个构造函数)。
示例
使用 Math.PI
下面的函数使用 Math.PI 来计算给定半径的圆的周长。
js
function calculateCircumference(radius) {
return Math.PI * (radius + radius);
}
calculateCircumference(1); // 6.283185307179586
规范
| 规范 |
|---|
| ECMAScript® 2026 语言规范 # sec-math.pi |
浏览器兼容性
加载中…