简体   繁体   English

console.log(String(console.log('Not undefined'))==='未定义'); console.log(String(console.log('Not undefined'))!=='Not undefined');

[英]console.log(String(console.log('Not undefined')) === 'undefined'); console.log(String(console.log('Not undefined')) !== 'Not undefined');

console.log(String(console.log('Not undefined')) === 'undefined');
console.log(String(console.log('Not undefined')) !== 'Not undefined');

I feel this two lines of codes are supposed to give me the false but ?if someone can explain to me?thanks 我觉得这两行代码应该给我错误的信息,但是?如果有人可以向我解释?

Let's break this apart into levels that will make this behavior cleaner. 让我们将其划分为多个级别,以使此行为更清晰。 First take the inner most command inside the first line: 首先在第一行中执行最里面的命令:

console.log('Not undefined')

Here the console.log function is echoing "Not undefined" but it is returning undefined . 在这里, console.log函数回显"Not undefined"但返回undefined This is the default behavior of all functions in JavaScript. 这是JavaScript 中所有函数默认行为 If they do not explicitly return something they will return the undefined value. 如果它们未显式返回某些内容,则它们将返回undefined值。 From there we step one level out to the casting of undefined to a string with this line: 从那里开始,我们一步一步地将undefined强制转换为带有此行的string

String(console.log('Not undefined'))

Which if we combine with the previous insight would appear like this to the JavaScript run-time: 如果我们结合先前的见解,JavaScript运行时将出现以下情况:

String(undefined)

This is evaluating to the string "undefined" . 这是对字符串"undefined"求值。 Next you are doing a literal comparison ( === compares values and types ) which evaluates to true . 接下来,您要进行字面比较( === 比较值和类型 ),其结果为true

The second line is the same, only now you are comparing that String(console.log('Not undefined')) is not 'Not undefined' which it is not so you get true as well. 第二行是一样的,只是你现在是比较String(console.log('Not undefined'))是不是'Not undefined'它是不是让您得到true为好。

As a matter of fact, it does. 事实上,确实如此。 If you run the code in developer tools, you'll notice that it returns three outputs: undefined, "undefined", and "Not undefined." 如果在开发人员工具中运行代码,则会注意到它返回三个输出:未定义,“未定义”和“未定义”。 The actual undefined result (not a string, it's really undefined) is from the console.log command itself - while console.log is able to print output to the console separately, it itself returns as undefined. 实际的未定义结果(不是字符串,它实际上是未定义的)来自console.log命令本身-虽然console.log能够将输出分别打印到控制台,但它本身会返回未定义的结果。 The "undefined" string comes from the String function, which appears to have an output of its own (not sure why). “未定义”字符串来自String函数,该函数似乎具有其自己的输出(不确定原因)。 Then, you get the desired result - "Not undefined". 然后,您将获得所需的结果-“未定义”。 So really, it does print the desired string to the console, but when you try to assign the code to a variable, like so: 因此,实际上,它确实将所需的字符串输出到控制台,但是当您尝试将代码分配给变量时,如下所示:

var myVar = console.log(String(console.log('Not undefined')));

myVar returns undefined. myVar返回未定义。 But, it does still print the output to the console. 但是,它仍然将输出打印到控制台。

console.log(String(console.log('Not undefined')) === 'undefined');

For the first one, you have from the deepest call: console.log('Not undefined') which obviously outputs Not undefined . 对于第一个,您可以从最深层次的调用中获得: console.log('Not undefined') ,它显然输出Not undefined Then the result (not the actual given string in the console) is given to the String() function which returns the string version of undefined which is 'undefined' . 然后将结果(不是控制台中实际的给定字符串)提供给String()函数,该函数返回undefined的字符串版本,即'undefined' All in all when compared to 'undefined' it returns true . 总而言之,与'undefined'相比,它返回true

For the other it's pretty much the same: 对于另一个,它几乎是相同的:

console.log(String(console.log('Not undefined')) !== 'Not undefined');

here console.log('Not undefined') just logs the string to the console but gives undefined to String() which converts it to "undefined" . 在这里console.log('Not undefined')只是将字符串记录到控制台, 但是将 undefined赋予String(),它将String转换为"undefined" Then compared to "Not undefined" , both are strings but they are different. 然后与"Not undefined"相比,两者都是字符串,但是它们是不同的。 It returns true . 返回true

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM