JavaScript Math.LN2 屬性



JavaScript 提供了 8 個數學常數,可以作為 Math 屬性訪問,而 "Math.LN2" 屬性就是其中之一。

JavaScript 的 **Math.LN2** 屬性用於表示 2 的自然對數,大約為 0.6931。此屬性等效於 Math.log(2)。

語法

以下是 JavaScript Math.LN2 屬性的語法:

Math.LN2

返回值

此屬性返回一個表示 2 的自然對數的數字。

示例 1

在以下示例中,我們使用 JavaScript Math.LN2 屬性來檢索 2 的自然對數:

<html>
<body>
<script>
   const result = Math.LN2;
   document.write(result);
</script>
</body>
</html>

輸出

執行上述程式後,我們將獲得一個大約為 0.6931 的值。

示例 2

Math.LN10 屬性等效於 Math.log(2):

<html>
<body>
<script>
   const result = Math.log(2);
   document.write(result);
</script>
</body>
</html>

輸出

如果我們執行上述程式,Math.log(2) 將返回與 Math.LN2 相同的結果。

廣告