Math.SQRT2

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

Math.SQRT2 靜態資料屬性表示 2 的平方根,約等於 1.414。

試一試

function getRoot2() {
  return Math.SQRT2;
}

console.log(getRoot2());
// Expected output: 1.4142135623730951

𝙼𝚊𝚝𝚑.𝚂𝚀𝚁𝚃𝟸=21.414\mathtt{Math.SQRT2} = \sqrt{2} \approx 1.414
Math.SQRT2 的屬性特性
可寫
可列舉
可配置

描述

Math.SQRT2 是一個常量,並且是 Math.sqrt(2) 的一個性能更高的等價替代方案。

由於 SQRT2Math 的一個靜態屬性,你始終應使用 Math.SQRT2,而不是作為你建立的 Math 物件(Math 不是建構函式)的屬性來使用。

示例

使用 Math.SQRT2

以下函式返回 2 的平方根

js
function getRoot2() {
  return Math.SQRT2;
}

getRoot2(); // 1.4142135623730951

規範

規範
ECMAScript® 2026 語言規範
# sec-math.sqrt2

瀏覽器相容性

另見