有什么值可以使 JavaScript 比较始终为真吗?
是否有任何 JavaScript 值可以使比较始终为真?
小于运算符的示例:
true < 10 true
false < 10 true
null < 10 true
大于运算符的示例:
true > 10 false
false > 10 false
null > 10 false
我要查找的内容:
alwaysTrue < 10 true
alwaysTrue > 10 true
我想用它来制作以下内容的一部分if 语句默认返回 true,当第一个比较值更改时返回 true 或 false。
这可能不存在,但我想完全确定。
Is there any JavaScript value that makes a comparison always true?
Example with lower than operator:
true < 10 true
false < 10 true
null < 10 true
Example with greater than operator:
true > 10 false
false > 10 false
null > 10 false
What I'm looking for:
alwaysTrue < 10 true
alwaysTrue > 10 true
I want to use this to make one part of an if statement return true by default and true or false when the first comparison value is changed.
This is probably not existent but I want to be completely sure.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能需要考虑在您的条件中利用“或”与另一个变量,该变量可以胜过它是否返回 true。
当 returnTrue 设置为 true 时,这将始终返回 true,否则它将依赖于比较。如果您只是寻找变量的更改,则可以通过存储旧值来实现。如果您知道它永远不会为空,您可以检查这一点,否则您可以使用带有类似于上面的标志的“或”。
You may want to consider leveraging "or" in your condition with another variable that can trump whether it returns true or not.
This will always return true when returnTrue is set to true, otherwise it will fall back on the comparison. If you are just looking for a change in a variable you can do so by storing the old value. If you know it will never be null you can check on this, otherwise you can use the the "or" with a flag similar to above.
我不太确定这是否是您所要求的,但这是一种通过更多语句来完成此操作的方法:
您也可以按照您的要求使用 AND 运算符来执行此操作:
if 语句执行以下操作 :我们:
无论哪种情况,测试表达式的右侧部分始终计算为 true,并且您只需输入if 当左侧部分也通过时。然而,在我看来,保持这一事实似乎有些过分了。
一旦你掌握了逻辑,就可以将其写成一行。
I'm not exactly sure if this is what you are asking, but this is a way of doing it with a few more statements:
You can also do this using the AND operator as you've requested:
The if statement does the following for us:
In either case the right part of the test expression always evaluates to true, and you'll only enter the if when the left part passes as well. However, keeping that true in place seems excessive to me.
Once you got you're logic in place this can go into one line.
不幸的是,我找不到任何东西。例如,在比较缺失的日期时,拥有这样的对象将非常有帮助。
然而,有些值始终为
false
:因此它们的否定将始终为
true
:因此您可以使用否定重写您的语句:
Unfortunately, I could not find any. Having such an object would be very helpful when comparing missing dates for instance.
There are however values that are always
false
:Hence their negation will be always
true
:You can hence rewrite your statement using the negation: