Get the App
SLTechnology News&Howtos  ›  Development  › 

How to avoid the use of = = by js

Shulou Source: shulou.com Published: 2022-06-01 05:29:01 09月20日 Update

Editor to share with you how to avoid the use of js, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Avoid the use of =

Here you may have questions, some people like to use =, some people like to use =, everyone's style is different, why do you force others to use =? People who are used to using = can't just because they hit the keyboard less than = =. Why not advocate the use of =?

(1) if you determine the type of variable, there is no need to use = =, as follows:

If (typeof num! = "undefined") {} var num = parseInt (value); if (num = = 10) {}

The above two examples are of certain types, one is a string and the other is an integer. There is no need to use =, just use =.

(2) if the type is uncertain, you should do the type conversion manually instead of letting others or later you guess that there is a type conversion, as follows:

Var totalPage = "5"; if (parseInt (totalPage) = 1) {}

(3) use = = does not pass when checking JSLint:

If (a = = b) {}

The output of the following JSLint:

Expected'= 'and instead saw' ='. If (a = = b) {

(4) and using = = may cause some strange phenomena, which may bury hidden dangers to the code:

Null = = undefined / / true''= ='0' / / false 0 = ='/ / true 0 = ='0' / / true''= = 0 / / true new String ("abc") = = "abc" / / true new Boolean (true) = = true / / true true = = 1 / / true

The above comparisons are all false when using =, which is more reasonable. For example, the first point that null should be equal to undefined is particularly strange, because null and undefined are two unrelated values, null should be used as an initialization null, and undefined is used to verify that a variable is undefined. This is similar to the idea of strong typing introduced in point 1.

The above is all the content of the article "how to avoid the use of js". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Type article necessary two content variable time phenomenon face not much code example most characters strings ideas manual integers more questions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft vpn OPPO Reno Huawei Shulou Information