>本文探討了JavaScript的內置對象,這是各種數學操作的功能寶庫。我們將檢查關鍵功能及其實際應用。 Math
>
鍵概念
本指南涵蓋:
Math
對象的綜合概述及其用於執行數學計算的功能。 我們將演示諸如Math
,Math.max
,Math.min
,Math.abs
,Math.pow
和Math.sqrt
等函數的使用。 Math.hypot
>
Math.sqrt
,Math.cbrt
,Math.log
,Math.log2
,Math.log10
和Math.hypot
>。
和Math.max
Math.min
>
。 JavaScript嘗試類型強制;例如,NaN
被脅迫到true
。 可以使用傳播操作員(1
)來處理陣列。 ...
>
Math.max(1, 2, 3, 4, 5); // 5 Math.min(4, 71, -7, 2, 1, 0); // -7 Math.max(...[8, 4, 2, 1]); // 8
示例:從數組中找到高分:
const scores = [23, 12, 52, 6, 25, 38, 19, 37, 76, 54, 24]; const highScore = Math.max(...scores); // 76
)Math.abs
Math.abs
Math.abs(5); // 5 Math.abs(-42); // 42 Math.abs(-3.14159); // 3.14159
計算兩個交易之間的節省:
const dealA = 150; const dealB = 167; const saving = Math.abs(dealA - dealB); // 17
Math.pow
>計算冪(基礎提高到指數)。啟動操作員()提供了等效功能。 >
Math.pow
**
Math.pow(2, 3); // 8 2 ** 3; // 8
)Math.sqrt
>
Math.cbrt
計算平方根,而
Math.sqrt
Math.cbrt
可以使用分數指數來計算其他根源:
Math.sqrt(4); // 2 Math.cbrt(1000); // 10
>對數(
,625 ** 0.25; // 5 (fourth root)
)
Math.log
(天然對數,basee),Math.log2
(base 2)和Math.log10
>(基本10)計算對數。
Math.max(1, 2, 3, 4, 5); // 5 Math.min(4, 71, -7, 2, 1, 0); // -7 Math.max(...[8, 4, 2, 1]); // 8
hypotenuse計算(Math.hypot
)
Math.hypot
計算右角三角形的斜邊(在兩個點之間的最短距離)。
const scores = [23, 12, 52, 6, 25, 38, 19, 37, 76, 54, 24]; const highScore = Math.max(...scores); // 76
>示例:在頁面上計算兩個點之間的距離:
Math.abs(5); // 5 Math.abs(-42); // 42 Math.abs(-3.14159); // 3.14159
本綜合指南為在JavaScript項目中利用
以上是有用的JavaScript數學功能以及如何使用它們的詳細內容。更多資訊請關注PHP中文網其他相關文章!