第 12 课 7 / 18 | 编码:L12-11
核心知识

显式类型转换

需要数字时明确使用 Number、parseInt 等转换,并检查无效结果。

  • Number('') 得到 0,要注意业务含义
  • Number('abc') 得到 NaN
  • Number.isNaN 检查非数值结果
const age = Number(ageInput.value);
if (Number.isNaN(age)) {
  message = '请输入数字';
}