简体   繁体   English

If 语句将 true 视为 false

[英]If statement treating true as false

I have stumbled across an extremely strange occurrence in Javascript that I can't make head or tail of.我在 Javascript 中偶然发现了一个我无法理解的极其奇怪的事件。

Here's a very simple if statement:这是一个非常简单的if语句:

let hours = 20;

I put a breakpoint here, and set hours = 0 in the debugger.我在这里放置了一个断点,并在调试器中设置hours = 0 I test !hours in the debugger to confirm the result is true , and click continue to run through the if statement.我在调试器中测试!hours以确认结果为true ,然后单击 continue 运行if语句。

if (!hours) {
  console.log("Hours is false: " + hours);
} else {
  console.log("Hours is true: " + hours);
}

Expected result to be logged:要记录的预期结果:

Hours is false: 0小时为假:0

Actual result logged:记录的实际结果:

Hours is true: 0小时为真:0

在此处输入图片说明

^ Mouse is hovering over hours so current value is visible ^ 鼠标悬停在hours因此当前值可见

This only happens when hours was originally set to an integer, and then set to 0 in the debugger.只有当hours最初设置为整数,然后在调试器中设置为 0 时才会发生这种情况。 Does Javascript have some obscure rule about truthy values retaining their status even after being changed? Javascript 是否有一些关于真实值即使在更改后仍保持其状态的模糊规则?

Or is this a discrepancy between the debugger and the code (which, if true, would basically defeat the point of the console)?或者这是调试器和代码之间的差异(如果为真,基本上会破坏控制台的观点)?

Why on earth is this happening?为什么会发生这种情况?

Check this one, open the devtools and run the code snippet, change the scope value to 0 or in the console put hours = 0. Both will work tested it检查这个,打开 devtools 并运行代码片段,将范围值更改为 0 或在控制台中放置 hours = 0。两者都可以测试它

Chrom version : Version 79.0.3945.88 (Official Build) (64-bit) Chrom 版本:版本 79.0.3945.88(官方版本)(64 位)

 let hours = 20; debugger if (!hours) { console.log("Hours is false: " + hours); } else { console.log("Hours is true: " + hours); }

Have you carefully parsed the hours received ?你仔细分析了收到的hours吗?

!0 === false
!"0" === true
!parseInt("0") === false

Have you computed string as if it was integer ?你有没有把字符串当作整数来计算?

"0" +  0  === "00"
 0  + "0" === 0
+"0" + 0  === 0
if (!hours) {
    console.log("Hours is false: " + hours);
} else {
    console.log("Hours is true: " + hours);
}

if hours = 1 then如果小时 = 1 那么

hours is true: 1小时是真的:1

or if hours = 0 then或者如果小时 = 0 那么

hours is false: 0小时为假:0

or if hours = 20 then或者如果小时 = 20 那么

hours is true: 20小时是真的:20

I put a breakpoint here, and set hours = 0 in the debugger.我在这里放置了一个断点,并在调试器中设置 hours = 0。

And that's probably the source of confusion.这可能是混淆的根源。 If your "here" is after the code tested the value of x , then your changing the value of x doesn't affect which branch gets executed.如果您的“此处”是代码测试x的值之后,那么您更改x的值不会影响执行哪个分支。

You have to check the data type:您必须检查数据类型:

typeof hours;

0 as number return false in if statement, that's why with ! 0 作为数字在 if 语句中返回 false,这就是为什么使用 ! it return true.它返回真。

var number = 0;
if (number) console.log(true); else console.log(false);
output -> false
if (!number) console.log(true); else console.log(false);
output -> true

That's clear JS.这很明显 JS。

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

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