javascript min max
时间: 2023-08-18 17:09:05 浏览: 54
在JavaScript中,可以使用Math对象的max和min方法来求一个数组中的最大值和最小值。其中,Math.max方法可以接受多个参数,返回这些参数中的最大值,而Math.min方法也可以接受多个参数,返回这些参数中的最小值。\[1\]
例如,在JavaScript代码中,可以使用Math.max方法来求一个数组中的最大值,如下所示:
```
let arr = \[3, 2, 45, 12\];
let max = Math.max.apply(null, arr);
console.log(max); // 输出结果为45
```
同样地,可以使用Math.min方法来求一个数组中的最小值,如下所示:
```
let arr = \[3, 2, 45, 12\];
let min = Math.min.apply(null, arr);
console.log(min); // 输出结果为2
```
以上代码中,通过apply方法将数组作为参数传递给Math.max和Math.min方法,从而求得数组中的最大值和最小值。\[1\] \[2\] \[3\]
#### 引用[.reference_title]
- *1* [javascript中max,min的用法](https://blog.csdn.net/double_sweet1/article/details/80328562)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* *3* [JavaScript中Math.max()和Math.min()方法](https://blog.csdn.net/weixin_45671901/article/details/127821265)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)