JS基础

39 阅读1分钟

一:随机数函数

得到0-1之间的小数: Math.random()

image.png

得到0~10之间的整数: parseInt(Math.random()*10)

image.png

得到[a.b]区间的整数:parseInt(Math.random()*(b-a+1))+a

得到[2,25]区间的整数

image.png

二:下标越界

JavaScript规定,访问数组中不存在的项会返回undefined,不会报错

var arr=['A','B','C','D'];

console.log(arr[4]); //undefined

console.log(arr[100000]);//undefined

console.log(arr[-1]);//undefined

image.png

三:数组类型的检测

数组使用typeof检测结果为object;typeof对于数组、对象和函数都返回"object",因此无法区分它们的具体类型,这时需要使用Array.isArray()方法来检测数组。

image.png