简体   繁体   English

不使用关系运算符的 JavaScript 条件表达式

[英]JavaScript Conditional expression without the use of Relational operator

Here is the prompt to start.这是启动的提示。

This is one way to code a conditional expression that tests whether a Boolean variable named isValid is true:这是编写测试名为 isValid 的布尔变量是否为真的条件表达式的一种方法:

isValid == true isValid == 真

Code another way that doesnt require the use of a relational operator.无需使用关系运算符的另一种编码方式。

How do you evaluate a boolean to be true without a relational operator?如果没有关系运算符,您如何评估布尔值是否为真? Has me a bit stuck是不是有点卡住了

Javascripts relational operators are : Javascripts 关系运算符是:

  • Greater than operator ( > )大于运算符 ( > )
  • Greater than or equal operator ( >= )大于或等于运算符 ( >= )
  • Less than operator ( < )小于运算符 ( < )
  • Less than or equal operator ( <= )小于或等于运算符 ( <= )
  • The equality operators ( == / === ) (and the inequality operators ( !== )等式运算符 ( == / === )(以及不等式运算符 ( !== )

One simple way to evaluate a variable as true or false based on it's "truthiness", without using relational operators, would be to use the !在不使用关系运算符的情况下,根据变量的“真实性”将变量评估为真或假的一种简单方法是使用! (not) operator twice (not)运算符两次

!!isValid

As "not thruthy" evaluates to false , and reverting it again as "not false" evaluates to true .因为“not thruthy”评估为false ,并且再次将其恢复为“not false”评估为true

Just use the expression by itself in any context that treats it as a boolean.只需在将其视为布尔值的任何上下文中单独使用表达式。 For instance.例如。

if (isValid) {
    ...
}

or或者

someVar = isValid ? "truevalue" : "falsevalue";

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

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