i was just going through the code of modenizer and came across the following function :
function contains(str, substr) {
return !!~('' + str).indexOf(substr);
}
modenizer has alot of such small function for small tests. now coming to my question , i do understand that double equal is for converting everything to boolean value , but what is !!~ for also what is the
''
before the str for ??
i have seen a few questions on SO that address similar issues but not exactly this issue , can somebody explain whats happening inside this function in the context of this example.
!!~('' + str)!!: Converts to boolean(true/false)~: Bitwise NOT Unary operator operates by reversing all the bits in the operand. Inverts the bits of its operand.Bitwise NOTing any number x yields -(x + 1).
'' + str: Type Casting Converts str tostringThe operator first casts
strtostringthen reverses the all the bits of binary and then returns the boolean result.EXAMPLE