目录
1. 运算符
1.1. typeof
当变量未定义,或变量已定义但未初始化时,typeof === undefined
判断有效
2. Math
2.1. Math.min 和 Math.max
- 定义
js 函数Math.min 和 Math.max 来查找二维数组中的最小值和最大值
- 问题1
在小数组上工作正常但 在大数组时报“Maximum call stack size exceeded.”
解决方案:
const arrayMinMax = (arr) => {
// Math.min 和 Math.max 在大数组时报“Maximum call stack size exceeded.”
return arr.reduce(
([min, max], val) => [Math.min(min, val), Math.max(max, val)],
[Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY]
);
};
3. 数据类型
3.1. 空值和假值
- undefined:表明变量没有初始化,即 “未定义”;
- null:js 关键字,用于描述 “空值”,表示数字、字符串、对象是 “无值” 的,typeof 为 object,但不具备对象实例的属性与方法;
- false、""、0:在 if 语句中表现为假值,他们都是有意义的数据,只是被用作空值或假值;
- 判断数据是否为空
typeof(false) === 'boolean'
typeof(0) === 'number'
typeof("") === 'string'
typeof(null) === 'object'
typeof(undefined) === 'undefined'