简体   繁体   English

chrome浏览器控制台为什么在JavaScript中将字符串强制转换为数字?

[英]Why does the chrome browser console forcibly coerce a string to a number in javascript?

noob question. 新手问题。 I'm learning 'You don't know JS' and I struck this anomaly on page 11: 我正在学习“您不知道JS”,我在第11页上发现了此异常:

var a = "42";
var b = Number( a );

console.log( a ); // "42"
console.log( b ); //  42

Now I do get that behaviour if I run it in JS Bin. 现在,如果我在JS Bin中运行它,便会获得该行为。 But in the latest Chrome browser console which I'm learning with, I get this unexpected behaviour: 但是在我正在学习的最新的Chrome浏览器控制台中,我得到了这种意外的行为:

var a = "42";
var b = Number(a);
console.log(a);
console.log(b);
42              // no quotes?
42
< undefined

And trying manual coercion from a number to a string, also an unexpected result: 尝试从数字到字符串手动强制转换,这也是意外的结果:

a = 42;
b = a.toString();
console.log(a);
console.log(b);
42      
42            // no quotes?
< undefined

Can anyone shed some light on this behaviour please? 有人可以阐明这种行为吗? I have googled it and come up short, cheers. 我用谷歌搜索了一下,加油。

The fact that the values aren't printed with quotes around them is due to the fact that the string doesn't contain any quotes as its content . 值中没有带引号的事实是由于字符串不包含任何引号作为其内容 So, you shouldn't expect quotes to be produced unless that were the case. 因此,除非是这种情况,否则您不应期望产生报价。

To remove any doubt, you can always check the type with typeof : 为了消除任何疑问,您始终可以使用typeof检查类型:

 var a = "42"; var b = Number(a); var c = '"String with quotes as part of the content."'; var d = 'String with NO quotes as part of the content.'; console.log(a, typeof a); console.log(b, typeof b); console.log(c, typeof c); console.log(d, typeof d); 

Keep in mind that the way a particular browser decides to implement their console behavior is entirely up to them. 请记住,特定浏览器决定实现其console行为的方式完全取决于他们。 There are no standards on this since console is not a part of JavaScript. 由于console不是JavaScript的一部分,因此没有任何标准。 So, it's entirely normal find differences between one client and another in that regard. 因此,在这方面,发现一个客户与另一个客户之间的差异是完全正常的。 What's important is that the JavaScript runtime process the data according to the standard. 重要的是JavaScript运行时根据标准处理数据。

When looking at the console you'll notice slight colour difference when logging it out. 当查看控制台时,注销时会发现轻微的色差。

A number will be blue whereas a string will be black. 数字将是蓝色,而字符串将是黑色。

If you don't want unexpected coercions just use === when doing comparisons or do what Scott Marcus suggests . 如果您不希望出现意外的胁迫,只需在进行比较或执行Scott Marcus建议的操作时使用===

The Chrome console.log(..) output is always without quotes. Chrome console.log(..)输出始终不带引号。 Try to see: 尝试看看:

console.log("asd")

And try this in the console: 并在控制台中尝试:

var a = "42";
a;

Should return the wanted result. 应该返回想要的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 javascript选择排序导致chrome浏览器控制台挂起或崩溃,为什么? - javascript selection sort causing chrome browser console to hang or crash why? Chrome浏览器控制台JavaScript参考 - Chrome browser console javascript reference 为什么 console.log(new Map<string, number> ([])) log true, true in JavaScript?</string,> - Why does console.log(new Map<String, Number>([])) log true, true in JavaScript? 为什么此Javascript在浏览器控制台中有效,但在Selenium的JavascriptExecutor中却无效? - Why does this Javascript work in the browser console but not in Selenium's JavascriptExecutor? 为什么新的Number(2)!= JavaScript中的新字符串(“2”) - Why does new Number(2) != new String(“2”) in JavaScript 为什么字符串到数字的比较在 Javascript 中起作用 - Why does string to number comparison work in Javascript 为什么Number &lt;String在JavaScript中返回true? - Why does Number < String returns true in JavaScript? 在Chrome等浏览器中停止控制台javascript命令 - Stop Console javascript commands in browser like chrome 为什么当我将这个数字记录到控制台时,JavaScript 会四舍五入? - Why does JavaScript round up this number when I log it to the console? 当我在浏览器中输入 console.log 时,为什么谷歌浏览器没有显示结果? - When I typed console.log to my browser, why does Google Chrome not showing me the result?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM