简体   繁体   English

Javascript:哪种方法最好检查条件语句?

[英]Javascript: Which way is better to check conditional statement?

In javascript, which will be better way to check conditional statement in terms of performance, robustness and which is the best practice? 在javascript中,哪种方法在性能,健壮性方面是检查条件语句的更好方法,并且哪种方法最佳?

var x = null;
// this condition is checked frequently and x will be set to null or some value accordingly
if(x !== null){...}
 OR
if(!!x){...}

You could just do 你可以做

if (x) { ... }

Simply says, if x is a truthy value. 简单地说,如果x是真实值。

As Nina pointed out. 正如尼娜指出的。 I would always add some extra validation, depending on what I'm expecting. 我总是会添加一些额外的验证,具体取决于我的期望。

if (x && x > 0) { ... }

Or 要么

if (x && x instanceof Array)

But I'm always checking x has some sort of value and isn't undefined or null 但我一直在检查x是否具有某种值并且不是undefined或为null

It's never necessary to write if(!!x){...} . 永远不需要编写if(!!x){...} That is exactly the same thing as writing if(x){...} . 这与编写if(x){...}完全相同。 Using the ! 使用! operator twice converts a value to a boolean that reflects whether it is "truthy" or not. 运算符两次将一个值转换为一个布尔值,以反映该值是否“真实”。 But the if statement does that anyway: it tests whether the value you provide is "truthy". 但是无论如何, if语句会这样做:它测试您提供的值是否“真实”。 So whether you use !! 因此,无论您使用!! or not it will do the same thing. 还是不,它会做同样的事情。

if(x !== null){...} is something else entirely. if(x !== null){...}完全是另外一回事。 It doesn't just check whether x is "truthy", it specifically compares the value of x with null . 它不仅检查x是否“真实”,还专门将x的值与null进行比较。 For example, if x has the numeric value 0 , if(x){...} or if(!!x){...} will not execute the ... code, because 0 is a "falsy" value, but if(x!==null) will execute the ... code, because 0 is not the same value as null . 例如,如果x的数值为0if(x){...}if(!!x){...}不会执行...代码,因为0是一个“假”值,但是if(x!==null) 执行...代码,因为0null

A related example is if(x!=null){...} or if(x==null){...} . 一个相关的示例是if(x!=null){...}if(x==null){...} (Note the use of != or == instead of !== or === .) This is a bit of a special case in JavaScript. (请注意,使用!===代替!===== 。)这是JavaScript中的一种特殊情况。 When you compare against null using the != or == operator, it actually compares against either null or undefined and treats those two values the same. 当你比较反对null使用!===操作符,它实际上比较反对任何nullundefined和对待这两个值相同。 In other words, if x is either null or undefined , then if(x==null){...} will execute the ... code, but if(x!=null){...} will not execute the ... code. 换句话说,如果x 要么是 nullundefined ,那么if(x==null){...}将执行...代码,但if(x!=null){...}将不执行...代码。

It's generally recommended to avoid the == and != operators and use the strict comparison === or !== instead, but treating null and undefined the same can be useful in some situations, so this is a case where the non-strict operators are helpful. 通常建议避免使用==!=运算符,而应使用严格的比较===!== ,但是在某些情况下将nullundefined视为相同可能会很有用,因此在这种情况下,非严格操作员会有所帮助。

In any case, the real question to ask is what is the purpose of your code, and what specifically do you need to test for here? 无论如何,真正要问的问题是代码的目的是什么,在这里您需要具体测试什么?

Assuming you need to check for a value which is strict inequality !== to null 假设您需要检查一个严格不等式的值!== null

this condition is checked frequently and x will be set to null or some value accordingly 经常检查此条件,并将x设置为null或相应的某个值

if (x !== null) { /* */ }

Then you can only use the above comparison. 然后,您只能使用上面的比较。 Any other comparison would return a wrong result. 任何其他比较都会返回错误的结果。

For example 例如

 var x = 0; if (x !== null) { // true console.log(x + ' !== null'); } if (x) { // false console.log(x); } // some more checks console.log(undefined !== null); // true console.log(undefined != null); // false 

Edit: My response was quite wrong which I learned after my conversation with Nina Scholz in her own response to this thread. 编辑:我的回答是很错误的,这是我在与Nina Scholz对话后对她自己的回应中得知的。 I updated accordingly. 我进行了相应的更新。

There are many ways to check values depending on the type you expect to use. 有多种检查值的方法,具体取决于您希望使用的类型。 Therefore the problem is not so much about performance as it is with correct evaluation. 因此,问题不仅仅在于性能,而在于正确的评估。 Starting with your examples: 从您的示例开始:

if(x != null)

This will evaluate to false if the value of x is either null or `undefined'. 如果x的值为null或“ undefined”,则该值为false

The next case: 下一种情况:

if(!!x)

This is an entirely different operation. 这是完全不同的操作。 It casts the value of x as a boolean, and in most cases if(x) will work the same way. 它将x的值强制转换为布尔值,并且在大多数情况下if(x)将以相同的方式工作。 But now, if the value is falsy, like 0, the empty string, the expression returns false . 但是现在,如果值是虚假的(如0),则为空字符串,则表达式返回false So, if expecting a number, you should check for the zero value: 因此,如果需要一个数字,则应检查零值:

if(x || (0 === x))

Bear in mind that if x were anything other than an int, the expression returns true. 请记住,如果x不是int,则表达式返回true。

And the case where you expect a string: 在您期望字符串的情况下:

if(x || ('' === x))

Same thing here, if x was, say 12, the expression returns true. 同样,如果x为12,则表达式返回true。

I could go on with lots of examples. 我可以举很多例子。 Unfortunately there are certain expressions that work in unexpected ways. 不幸的是,某些表达式以意外的方式起作用。 For instance, it should be easy to check if a value is a number by calling isNaN(number), but if the value is an empty string or a representation of an array index (' 2 '), isNaN returns false. 例如,通过调用isNaN(number)可以很容易地检查一个值是否为数字,但是如果该值为空字符串或数组索引的表示形式(“ 2 ”),则isNaN返回false。

I recommend you to check this table with all the type conversions so that you become more aware on how to check the validity of a value. 我建议您检查所有类型转换的 ,以便您更加了解如何检查值的有效性。

暂无
暂无

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

相关问题 在JavaScript中的单个if语句中检查多个OR条件的更好方法 - Better way to check multiple OR condition within single if statement in javascript 检查网络对帐单的更好方法? - A Better way to check network statement? 对于这个 if/else 语句,JavaScript 条件运算符是更好的方法吗? - Is JavaScript Conditional Operator better method for this if/else statement? 如何在 JavaScript 的条件语句中检查对象属性? - How to check for object properties in a conditional statement in JavaScript? JavaScript 条件语句检查空返回字符串 - JavaScript conditional statement with check for empty returns string 有没有更好的方法在javascript中编写return语句 - is there a better way to write return statement in javascript 有没有更好的方法来编写这个 switch 语句 javascript - is there a better way to write this switch statement javascript 有没有更好的方法在 javascript if 语句中“什么都不做”? - Is there a better way to 'do nothing' in javascript if statement? 是否有更好/更短的方法来检查数组中对象的值,该数组是 Javascript 中对象的一部分? - Is there a better/shorter way to check a value of an object within an array which is part of an object in Javascript? 如果大部分函数的执行以输入为条件,那么哪一个是更好的实现方式? - If the execution of the bulk of a function is conditional on the input, which of these is a better way to implement?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM