有什么值可以使 JavaScript 比较始终为真吗?

发布于 2024-11-05 03:06:19 字数 536 浏览 1 评论 0原文

是否有任何 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

美人迟暮 2024-11-12 03:06:19

您可能需要考虑在您的条件中利用“或”与另一个变量,该变量可以胜过它是否返回 true。

returnTrue || testVariable < 10

当 returnTrue 设置为 true 时,这将始终返回 true,否则它将依赖于比较。如果您只是寻找变量的更改,则可以通过存储旧值来实现。如果您知道它永远不会为空,您可以检查这一点,否则您可以使用带有类似于上面的标志的“或”。

oldValue === null || currentValue === oldValue

You may want to consider leveraging "or" in your condition with another variable that can trump whether it returns true or not.

returnTrue || testVariable < 10

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.

oldValue === null || currentValue === oldValue
趁年轻赶紧闹 2024-11-12 03:06:19

我不太确定这是否是您所要求的,但这是一种通过更多语句来完成此操作的方法:

var rtn = true;

if (oldCompareValue != newCompareValue) {
   // next you'll want to insert your return expression
   // i'm not sure you've specified what this should be
   rtn = (newCompareValue > 10)? true: false;
}

return rtn;

您也可以按照您的要求使用 AND 运算符来执行此操作:

rtn = true;

if ((oldCompareValue != newCompareValue) && true) {
   rtn = (newCompareValue > 10)? true: false;
}

return rtn;

if 语句执行以下操作 :我们:

  1. 如果 oldCompareValue 与 newCompareValue 相同,则整个语句为 false
  2. 如果 oldCompareValue 与 newCompareValue 不同,则整个语句为 true

无论哪种情况,测试表达式的右侧部分始终计算为 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:

var rtn = true;

if (oldCompareValue != newCompareValue) {
   // next you'll want to insert your return expression
   // i'm not sure you've specified what this should be
   rtn = (newCompareValue > 10)? true: false;
}

return rtn;

You can also do this using the AND operator as you've requested:

rtn = true;

if ((oldCompareValue != newCompareValue) && true) {
   rtn = (newCompareValue > 10)? true: false;
}

return rtn;

The if statement does the following for us:

  1. if oldCompareValue is the same as newCompareValue then the whole statement is false
  2. if oldCompareValue is not the same as newCompareValue then the whole statement is true

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.

滴情不沾 2024-11-12 03:06:19

不幸的是,我找不到任何东西。例如,在比较缺失的日期时,拥有这样的对象将非常有帮助。

然而,有些值始终为 false

NaN > 10 // false
NaN < 10 // false
undefined < 10 // false
undefined > 10 // false

因此它们的否定将始终为 true

!(NaN > 10) // true
!(NaN < 10) // true
!(undefined < 10) // true
!(undefined > 10) // true

因此您可以使用否定重写您的语句:

value > 10

!(value < 10)

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:

NaN > 10 // false
NaN < 10 // false
undefined < 10 // false
undefined > 10 // false

Hence their negation will be always true:

!(NaN > 10) // true
!(NaN < 10) // true
!(undefined < 10) // true
!(undefined > 10) // true

You can hence rewrite your statement using the negation:

value > 10

!(value < 10)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文