javascript - 下面的代码将向控制台输出什么,为什么?
问题描述
我正在将输出输出到下面的代码,但我不知道它是如何工作的,谁能告诉我输出的原因是什么。
console.log(1 + "2" + "2");
console.log(1 + +"2" + "2");
console.log(1 + -"1" + "2");
console.log(+"1" + "1" + "2");
console.log("A" - "B" + "2");
console.log("A" - "B" + "2");
解决方案
console.log(1 + "2" + "2"); // it is concat as string
console.log(1 + +"3" + "2"); // 2nd + makes 2 as string to integer. but second 2 is string. so it makes 42
console.log(1 + -"1" + "2");// 2nd - makes 1 as string to integer. but second 2 is string. so it makes 02
console.log(+"1" + "1" + "2");// 1st - makes 1 as string to integer. and same behaviour of the first logging
console.log("A" - "B" + "2"); // - cannot used for string values. nor this reason find A - B is NAN and adding 2 as string.
//console.log("A" - "B" + "2); // syntax eerror. second " in 2 missing
推荐阅读
- python - 如何使用 Python 处理带中断的双数组?
- flutter - 如何在 Flutter 中为我的标准开关添加渐变?
- amazon-web-services - 如何使 apigateway 转发根路径到集成的 http 端点?
- asp.net-mvc - IIS 与 IIS Express 会话处理和多用户
- mysql - 分隔符问题
- list - 根据另一个列表从列表中删除和添加 Ints
- java - 哪种方法更适合使用 Docker 和 Jenkins 构建 Java 代码?
- python - 如何使用保存的模型来恢复谷歌 colab 中的训练?
- asp.net-core - Angular 11 本地化 (i18n) 发布问题
- c++ - 构建 C++ GRPC 时出错