Math
[TOC]
索引
静态属性:
- Math.PI:
≈3.141592653589793
,只读,表示数学常数 圆周率 π 的近似值(约3.14159
)。
静态方法:
随机数:
- Math.random():
()
,用于生成一个 [0, 1) 区间(包含 0 但不包含 1)的伪随机浮点数。
取整:
- Math.floor():
(x)
,用于将数字向下取整(向负无穷方向取整)。 - Math.ceil():
(x)
,用于将数字向上取整(向正无穷方向取整)。 - Math.round():
(x)
,用于执行四舍五入取整操作。 - Math.trunc():
(x)
,ES2015,用于直接去除数字的小数部分,返回该数字的整数部分(无论正负)。
最值与绝对值:
- Math.min():
(value1, value2, ..., valueN)
,用于返回一组数字中的最小值。 - Math.max():
(value1, value2, ..., valueN)
,用于返回一组数字中的最大值。 - Math.abs():
(x)
,用于返回一个数字的绝对值(即该数字的非负形式)。
指数运算:
- Math.pow():
(base, exponent)
,用于计算一个数的指定次幂(即 baseexponentbaseexponent)。支持正负基数、分数指数和特殊值处理。
Math
静态属性
PI
Math.PI:≈3.141592653589793
,只读,表示数学常数 圆周率 π 的近似值(约 3.14159
)。
静态方法
随机数
random()@
Math.random():()
,用于生成一个 [0, 1) 区间(包含 0 但不包含 1)的伪随机浮点数。
返回:
num:
number
,每次调用返回新的伪随机数(基于确定性算法生成),不适合加密场景。
核心特性:
不可预测但可重现:
序列由隐式种子决定(通常基于时间),但无法手动设置种子。
js// 连续调用产生不同结果 console.log(Math.random()); // 0.123456789 console.log(Math.random()); // 0.987654321
引擎依赖性 不同 JavaScript 引擎(V8/SpiderMonkey/JavaScriptCore)实现算法不同:
- 历史上:线性同余生成器(LCG)
- 现代引擎:通常使用 xorshift128+ 等更高级算法
不适用于安全场景
在游戏开发、数据抽样等非安全场景优先使用
Math.random()
;如需加密安全随机数(身份验证、加密密钥等),请使用 Web Crypto API:crypto.getRandomValues()
js// 生成安全随机数(0-255) const array = new Uint8Array(1); crypto.getRandomValues(array); console.log(array[0]);
示例:
生成 [0, max) 的随机数:
Math.random() * max
jsfunction getRandomInt(max) { return Math.floor(Math.random() * max); } console.log(getRandomInt(100)); // 0-99 的整数
生成 [min, max) 的随机数:
Math.random() * (max - min) + min
生成 [min, max] 的随机整数:
Math.floor(Math.random() * (max - min + 1)) + min
jsfunction getRandomInRange(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } console.log(getRandomInRange(5, 10)); // 5~10 的整数
取整
floor()@
Math.floor():(x)
,用于将数字向下取整(向负无穷方向取整)。
x:
number
,需要取整的数字。支持整数、浮点数、负数、数值字符串(如"5.7"
)等。返回:
num:
number
,返回小于或等于给定数字的最大整数。
核心特性:
向下取整方向:负无穷
正数:直接舍弃小数部分
jsMath.floor(5.9) // 5
负数:取更小的整数(向负无穷)
jsMath.floor(-3.1) // -4(比 -3.1 小的最大整数是 -4)
自动类型转换:
非数值参数会尝试转换为数字:
jsMath.floor("12.8") // 12(字符串 → 数字) Math.floor([5.7]) // 5(数组 → 字符串 "5.7" → 数字 5.7 → 取整)
示例:
基本使用:
js// 特殊值 Math.floor(5.95) // 5 Math.floor(-3.2) // -4(向下取整到更小的整数) Math.floor("8.9") // 8(自动转换字符串为数字) Math.floor(null) // 0(null → 0) Math.floor(true) // 1(true → 1) Math.floor(false) // 0(false → 0) // 无效值返回 NaN Math.floor("abc") // NaN Math.floor(undefined) // NaN // 边界值 Math.floor(5) // 5(整数不变) Math.floor(0) // 0 Math.floor(-0) // -0 Math.floor(Infinity) // Infinity Math.floor(-Infinity) // -Infinity
配合随机数生成范围整数:生成
[min, max]
的随机整数js// 生成 [min, max] 的随机整数 function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } console.log(getRandomInt(3, 7)); // 3,4,5,6,7
ceil()@
Math.ceil():(x)
,用于将数字向上取整(向正无穷方向取整)。
x:
number
,需要取整的数字。支持整数、浮点数、负数、数值字符串(如"5.1"
)等。返回:
num:
number
,返回大于或等于给定数字的最小整数。
核心特性:
向上取整方向:正无穷
正数:无论小数部分多小都进一
jsMath.ceil(2.01) // 3
负数:向零方向取整(更大的负整数)
jsMath.ceil(-3.7) // -3(比 -3.7 大的最小整数是 -3)
自动类型转换:
非数值参数会优先隐式转换为数字:
jsMath.ceil("4.2") // 5(字符串 → 数字) Math.ceil([2.1]) // 3(数组 → 字符串 "2.1" → 数字 2.1 → 取整)
示例:
基本使用:
jsMath.ceil(5.1) // 6 Math.ceil(-3.7) // -3(向上取整到更大的整数) Math.ceil("7.2") // 8(自动转换字符串为数字) Math.ceil(null) // 0(null → 0) Math.ceil(true) // 1(true → 1) Math.ceil(false) // 0(false → 0) // 无效值返回 NaN Math.ceil("text") // NaN Math.ceil(undefined) // NaN // 边界值 Math.ceil(7) // 7(整数不变) Math.ceil(0) // 0 Math.ceil(-0) // -0 Math.ceil(Infinity) // Infinity Math.ceil(-Infinity) // -Infinity
round()
Math.round():(x)
,用于执行四舍五入取整操作。
x:
number
,需要取整的数字。支持整数、浮点数、负数、数值字符串(如"5.5"
)等。返回:
num:
number
,返回与给定数字最接近的整数,遵循"四舍五入"规则。
核心特性:
四舍五入规则:
小数部分 < 0.5 → 向下取整
jsMath.round(2.4) // 2
小数部分 > 0.5 → 向上取整
jsMath.round(2.6) // 3
小数部分 = 0.5 → 向正无穷方向取整(关键!不同于数学实现)
jsMath.round(2.5) // 3(正数向上) Math.round(-2.5) // -2(不是-3!负数向0方向)) Math.round(-2.6) // -3
自动类型转换:
非数值参数会隐式转换为数字:
jsMath.round("4.6") // 5 Math.round([2.49]) // 2(数组→字符串"2.49"→数字2.49→取整)
示例:
基本使用:
jsMath.round(5.95) // 6 Math.round(5.5) // 6(小数部分=0.5时向正无穷取整) Math.round(-1.5) // -1(不是-2!负数0.5向正无穷取整即向0方向) Math.round("8.9") // 9(字符串→数字) Math.round(null) // 0(null→0) Math.round(true) // 1(true→1) // 无效值返回 NaN Math.round("text") // NaN Math.round(undefined) // NaN // 边界值 Math.round(7) // 7(整数不变) Math.round(0.1) // 0 Math.round(-0.9) // -1 Math.round(Infinity) // Infinity Math.round(-Infinity) // -Infinity Math.round(-0) // -0
trunc()
Math.trunc():(x)
,ES2015,用于直接去除数字的小数部分,返回该数字的整数部分(无论正负)。
x:
number
,需要截断的数字。支持整数、浮点数、负数、数值字符串(如"5.9"
)等。返回:
num:
number
,返回该数字的整数部分(无论正负)。
核心特性:
简单截断机制:直接删除小数部分(无论正负)
jsMath.trunc(7.9) // 7 Math.trunc(-4.2) // -4
无舍入行为:
与
Math.floor()
/Math.ceil()
不同,不进行任何舍入计算:jsMath.trunc(2.999) // 2(不四舍五入) Math.trunc(-1.999) // -1(不向下取整)
自动类型转换:
非数值参数会隐式转换为数字:
jsMath.trunc("8.6") // 8 Math.trunc([-3.7]) // -3(数组→字符串"-3.7"→数字-3.7→截断)
示例:
基本使用:
jsMath.trunc(13.37) // 13 Math.trunc(-3.14) // -3(直接去除小数部分) Math.trunc("42.8") // 42(字符串→数字) Math.trunc(null) // 0(null→0) Math.trunc(true) // 1(true→1) Math.trunc(false) // 0(false→0) // 无效值返回 NaN Math.trunc("text") // NaN Math.trunc(undefined) // NaN // 特殊值 Math.trunc(0) // 0 Math.trunc(-0) // -0 Math.trunc(Infinity) // Infinity Math.trunc(-Infinity) // -Infinity
最值与绝对值
min()
Math.min():(value1, value2, ..., valueN)
,用于返回一组数字中的最小值。
value1, value2, ..., valueN:
number
,0-n个需要比较的数字。支持整数、浮点数、数值字符串、null
(转为0)返回:
num:
number
,返回值:最小数值
:有效参数>=1个,返回参数中的最小值。Infinity
:无参数,空集合的最小值定义为正无穷。NaN
:任意参数无法转为有效数字或参数包含NaN时返回 NaN。-Infinity
:若参数包含-Infinity
则必为最小值。
核心特性:
特殊值优先级:
NaN
>-Infinity
> 普通数字 >Infinity
:jsMath.min(NaN, -Infinity) // NaN(NaN优先级最高) Math.min(-Infinity, 10) // -Infinity Math.min(Infinity, 100) // 100
自动类型转换:
所有参数会尝试转换为数字:
jsMath.min("5", 3, "2.8") // 2.8(字符串→数字) Math.min([1], [0]) // 0(数组→字符串"1"→数字1)
空参数行为:
无参数时返回理论最大值
Infinity
:jsMath.min() // Infinity(空集的最小值定义为正无穷)
单参数行为:
相当于强制类型转换:
jsMath.min(5) // 5 Math.min("abc") // NaN
数组参数需解构:
直接传入数组无效:
jsMath.min([1, 2, 3]) // NaN(数组被视为单个对象) // 正确做法 Math.min(...[1, 2, 3]) // 1
示例:
基本使用:
js// 常规比较 Math.min(10, 20, -5, 7) // -5 // 类型转换 Math.min("15", "2.5", -3) // -3(字符串→数字) Math.min(null, 5, -2) // -2(null→0) Math.min(true, false) // 0(true→1, false→0) // 无效值导致 NaN Math.min(10, "text", 5) // NaN(存在无效数字) Math.min(NaN, 8) // NaN
max()
Math.max():(value1, value2, ..., valueN)
,用于返回一组数字中的最大值。
- value1, value2, ..., valueN:
number
,0-n个需要比较的数字。支持整数、浮点数、数值字符串、null
(转为0) - 返回:
- num:
number
,返回最大的值:最大数值
:有效参数>=1个,返回参数中的最大值。-Infinity
:无参数,空集合的最大值定义为负无穷。NaN
:任意参数无法转为有效数字或参数包含NaN时返回 NaN。Infinity
:若参数包含Infinity
则必为最大值。
核心特性:
自动类型转换:
所有参数会尝试转换为数字:
jsMath.max("5", 3, "8.2") // 8.2(字符串→数字) Math.max([1], [0]) // 1(数组→字符串"1"→数字1)
特殊值优先级:
NaN
>Infinity
> 普通数字 >-Infinity
:jsMath.max(NaN, Infinity) // NaN(NaN优先级最高) Math.max(Infinity, 10) // Infinity Math.max(-Infinity, 100) // 100
空参数行为:
无参数时返回理论最小值:
jsMath.max() // -Infinity(空集的最大值定义为负无穷)
单参数行为:
相当于强制类型转换:
jsMath.max(5) // 5 Math.max("abc") // NaN
数组参数需解构:
直接传入数组无效:
jsMath.max([1, 2, 3]) // NaN(数组被视为单个对象) // 正确做法 Math.max(...[1, 2, 3]) // 3
示例:
基本使用:
js// 常规比较 Math.max(10, 20, -5, 7) // 20 // 类型转换 Math.max("15", "25", 10) // 25(字符串→数字) Math.max(null, 5, -2) // 5(null→0) Math.max(true, false) // 1(true→1, false→0) // 无效值导致 NaN Math.max(10, "text", 5) // NaN(存在无效数字) Math.max(NaN, 8) // NaN
abs()
Math.abs():(x)
,用于返回一个数字的绝对值(即该数字的非负形式)。
x:
number
,需要计算绝对值的数字。支持整数、浮点数、负数、数值字符串、null
等。返回:
num:
number
,返回一个非负数。
核心特性:
自动类型转换:
非数值参数会隐式转换为数字:
jsMath.abs("7") // 7(字符串→数字) Math.abs([-4.2]) // 4.2(数组→字符串"-4.2"→数字-4.2→绝对值)
示例:
基本使用:
jsMath.abs(5) // 5 Math.abs(-5.7) // 5.7 Math.abs("-3.2") // 3.2(字符串→数字) Math.abs(null) // 0(null→0) Math.abs(true) // 1(true→1) Math.abs(false) // 0(false→0) // 无效值返回 NaN Math.abs("text") // NaN Math.abs(undefined) // NaN // 特殊值 Math.abs(0) // 0 Math.abs(-0) // 0(负零转为0) Math.abs(Infinity) // Infinity Math.abs(-Infinity) // Infinity Math.abs(NaN) // NaN
指数运算
pow()
Math.pow():(base, exponent)
,用于计算一个数的指定次幂(即 baseexponentbaseexponent)。支持正负基数、分数指数和特殊值处理。
base:
number
,基数(要计算幂的数字)。exponent:
number
,指数(底数要乘方的次数)。返回:
num:
number
,返回基数的指数次幂的结果。
核心特性:
自动类型转换:
非数值参数尝试转换为数字:
jsMath.pow("4", "2") // 16 Math.pow([2], [3]) // 8(数组→字符串"2"→数字2)
负底数限制:
负底数仅支持整数指数:
jsMath.pow(-2, 3) // -8(整数指数允许) Math.pow(-4, 0.5) // NaN(非整数指数禁止)
小数指数计算:
支持分数指数(等价于开方):
jsMath.pow(27, 1/3) // 3(³√27) Math.pow(16, 0.25) // 2(⁴√16)
ES2016+ 的指数运算符 (
**
)现代JS推荐使用更简洁的
**
:js2 ** 3 // 8(等价于 Math.pow(2, 3)) -8 ** (1/3) // 语法错误,需括号:(-8) ** (1/3)
示例:
基本使用:
js// 常规计算 Math.pow(2, 3) // 8(2³) Math.pow(4, 0.5) // 2(√4) Math.pow(8, -1) // 0.125(1/8) // 类型转换 Math.pow("2", "3") // 8(字符串→数字) Math.pow(null, 2) // 0(null→0) Math.pow(true, 2) // 1(true→1) // 无效值 Math.pow("a", 2) // NaN(转换失败) Math.pow(2, {}) // NaN(对象无法转换) // 边界值 Math.pow(0, 0) // 1(数学上未定义,JS标准规定为1) Math.pow(0, 5) // 0 Math.pow(0, -2) // Infinity Math.pow(Infinity, 0) // 1