简体   繁体   English

Javascript - 为什么这个 if 语句会在控制台中打印两次“Hello”?

[英]Javascript - Why does this if statement print "Hello" twice in the console?

For this sample code, it prints "Hello" twice into the console:对于此示例代码,它会在控制台中打印两次“Hello”:

 var x = 10; if ( (null) || (console.log("Hello")) || x > 5 ) { console.log("Hello"); }

I assume when the "if" statement is read, it reads the console.log, then prints it, and since x > 5, the return statement is true, then consoles the second "Hello".我假设当“if”语句被读取时,它读取console.log,然后打印它,因为x> 5,返回语句为真,然后控制台第二个“Hello”。

Can someone tell me if this is correct?有人能告诉我这是否正确吗? Thanks!谢谢!

Your function:你的功能:

  1. Sets variable and it's value var x = 10设置变量及其值var x = 10

  2. Checks the conditions, until one of them "is" true (returns true )检查条件,直到其中之一“是” true (返回true

  3. (null) returns itself, so the functions checks other condition (null)返回自身,因此函数会检查其他条件
  4. (console.log("Hello")) returns nothing ( null ), so the function checks other condition (console.log("Hello"))返回任何内容( null ),因此该函数会检查其他条件

  5. x > 5 returns true , so the execution of code inside if{ < code > } happens x > 5返回true ,因此if{ < code > }的代码执行发生

  6. (console.log("Hello")) inside if{ } executes. (console.log("Hello")) if{ }执行。

As you can see, the (console.log("Hello")) is executed both, while checking conditions inside if statement and while executing "the insides" of that if .正如你所看到的, (console.log("Hello"))执行两种,而检查内部情况if声明,而执行的是“内侧” if That's why you see "Hello" logged 2 times.这就是为什么您会看到“Hello”记录了 2 次。

console.log has a return type of undefined. console.log 的返回类型为 undefined。

You're seeing the console.log output twice, because you've called it twice:您会看到两次 console.log 输出,因为您已经调用了两次:

Once in your if condition, and the next in your if statement.一次在您的 if 条件中,然后在您的 if 语句中。

Your if statement will run because you're using OR, and x = 10 > 5您的 if 语句将运行,因为您使用的是 OR,并且 x = 10 > 5

The if statement first encounters null , which is false, then does a console.log() , which returns undefined so is also false, and finally x > 5 , which is true. if语句首先遇到null ,它是假的,然后执行console.log() ,它返回undefined所以也是假的,最后x > 5 ,这是真的。 Thus the if statement is true, and it goes into the body and does another print.因此if语句为真,它进入主体并进行另一次打印。

I assume when the "if" statement is read, it reads the console.log, then prints it, and since x > 5, the return statement is true, then consoles the second "Hello".我假设当“if”语句被读取时,它读取console.log,然后打印它,因为x> 5,返回语句为真,然后控制台第二个“Hello”。

More accurately, the "if" statement is executed .更准确地说,是执行“if”语句。 Specifically, the condition is evaluated.具体而言,评估条件。 Since null is "falsey", the right of the ||由于null是“falsey”,所以||的右边operator is evaluated.运算符进行评估。 Which executes the first console.log() .它执行第一个console.log() This in turn returns undefined which is also "falsey" and the next operand of the next ||这反过来返回undefined这也是“falsey”和下一个||的下一个操作数is evaluated.被评估。 x>5 evaluates to true , so the second console.log() is executed. x>5评估为true ,因此执行第二个console.log()

Note that this is not a very good way to write code.请注意,这不是编写代码的好方法。 console.log() shouldn't be used inside an if condition like this. console.log()不应该在像这样的if条件中使用。 It's return value is meaningless since it always returns undefined .它的返回值毫无意义,因为它总是返回undefined

The first console.log you wrote is in the if statement, that function will be called when the if statement is evaluated.你写的第一个 console.log 是在 if 语句中,当 if 语句被评估时,该函数将被调用。 The second statement will be called if the condition in the if statement is met.如果满足 if 语句中的条件,将调用第二个语句。 The console.log itself returns undefined and has no real purpose to be in your if statement. console.log 本身返回 undefined 并且在你的 if 语句中没有真正的目的。 I would recommend removing it as I don't believe it is achieving what you intended Good luck :)我建议将其删除,因为我认为它无法实现您的预​​期 祝您好运 :)

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

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