javascript中数组中求最大值示例代码

Javascript教程 2025-08-11

复制代码代码如下:

< html >

< head >

< title >数组的最大值的获取< /title >

< script >

//定义数组

var arr = [1,4,3,9,5,0,-1,7,22];

//最大值的下标,先假定为第一个元素的下标

var index = 0;

for(var x = 0; x <  arr.length; x++){

if(arr[index] <  arr[x]){

index = x;

}

}

document.write("索引为" + index + "中的" + arr[index] + "最大");

< /script >

< /head >

< body >

< div id="time" >< /div >

< /body >

< /html >