[solved] in js! Is it necessary to exist? Why?

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:

    1. ! 0 = > true
    2. ! true = > false
  • my steps

    1. 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?
Apr.27,2021

is used for type conversion, casting to Boolean values.

as to why it is needed. Let me elaborate on this:

  

it exists to force any value to be Boolean as mentioned above.

Menu