Business background
< hr >- browse the `lodash source code
- found
!!
this usage - look it up, it means to take it backwards twice
- but don"t understand the necessity of his existence
sample code
< hr >official source code of lodash
function isObjectLike(value) {
return !!value && typeof value == "object";
}
my modified code, that is, remove !!
directly
function isObjectLike(value) {
return value && typeof value == "object";
}
my confusion
< hr >-
meaning of official code
- means to take the reverse and then reverse it, and finally judge whether the variable is true
-
my code
-
js
will automatically judge whether it is true or false based on the data type and the value of the variable
-
for example, send a 0 in
-
official steps:
- ! 0 = > true
- ! true = > false
-
my steps
- 0 = > false\
question
< hr >- since the results are all the same, why is the code written by many bosses used in this way? It"s definitely not superfluous, is it?