Spot 类型 Coercion Bug
找错误 中等
题目
这个 code 是 supposed 到添加 10 and 20, 但它 prints "1020". 什么是 错误?
代码
let a = "10";
let b = 20;
console.log(a + b);
提示
检查 whether 是字符串 or 数字.
解释
Because ` is the string "10", the + operator concatenates instead of adding. "10" + 20 gives "1020". The fix is to convert a to a number: let = 10; or 数字() + b`. This is a classic JavaScript type coercion pitfall.
相关资源
相关课程
- 数据类型
学习关于基本数据类型 in programming.
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。