ActionScript: Should I always use strict equality ("===")?

I am wondering if I should always use "===" (strict equality) when performing equality checks ... Is there an example of when it is preferable to use "==" (not strict equality)? In particular, it should:

  • if (param1 == null || param1.length == 0)

will be

  • if (param1 === null || param1.length === 0) ?

How about things like strings? param1 == "This is a String."

+4
source share
2 answers

The operator to be used depends on your needs.

"==" checks if two values ​​are equal after they have been converted to the same data type (if possible). Thus, "5" == 5 will be true, since the string "5" is converted to a number, and then a check is made, and obviously 5 is really 5.

"===" checks if two values ​​are the same AND types and equal. Thus, "5" === 5 will evaluate to false because one is a string and one is a number.

In terms of choice of use, it comes down to expectations. If you expect two values ​​that you are comparing with the same type, you should use "===". However, if they can be of different types and you want the comparison to perform automatic conversion (for example, comparing line 5 with number 5), you can use "==".

In the case of your examples, everything should be fine with the "==" operator, but for security of the added type, you can certainly use the "===" operator. For example, I usually check for zeros.

Hope this helps.

+5
source

I really don't know much about ActionScript, but I find it compatible with EMCAScript, which means that my knowledge of JavaScript will make a difference.

In JavaScript, if you want to accept explicit typing (it never does "something" + 5 , it should always be explicit in your types, for example "something" + String(5) ), if you use this approach, you will never actually have an instance where == is preferred ===.

+1
source

Source: https://habr.com/ru/post/1340479/


All Articles