Understanding modenizer contains a feature

i just looked through the modenizer code and came across the following function:

function contains(str, substr) {
    return !!~('' + str).indexOf(substr);
}

modenizer has such a small function for small tests. Now, turning to my question, I understand that double equal is the conversion of everything to a logical value, but what is it !!~for what it is

''  

up to strfor?

I saw several SO questions that address similar problems, but not exactly this problem, can someone explain what is happening inside this function in the context of this example.

+4
source share
2 answers

!!~('' + str)

  • !!: converts to a boolean value ( true/false)
  • ~: NOT , , .

    x - (x + 1).

  • '' + str: Casting str string

str string, .

contains('abcd', 'd');

1. If str is not string then it is converted to string

    true + '' // "true"
    1 + ''    // "1"

2. `indexOf`
    The index of `substr` is returned.

3. `~`
    The bitwise NOT of 3 which is -(3 + 1) = -4

4. `!!`
    !!-4 = true


true will be returned.
+2

. ~ x . -1 0. !! ~ ' -1' .

+2

All Articles