Closed danilosampaio closed 3 years ago
~
returns the bitwise complement. I guess since JS doesn't have real integers this isn't particularly useful, but in other languages that's what ~
is for.
Putting almost any arithmetic operator in front of an expression in JS will cause the value to convert to a Number, put a "+" or "-" instead of the tilde and you will get similar results. The only valid thing is the indexOf, but in the same manner you can do 1+someStr.indexOf("a")
This is more like a hack. Looks pretty expectable, so I'd prefer not to add it. Anyway, thanks for reporting!
Tilde operator (~): -(N+1)
where N is the expression to the right of the tilde
Uses:
String to number:
console.log(~~"1"); // 1
true/false to 1/0:
console.log(~~true); // 1
console.log(~~false); // 0
Array/String contains a element/char:
~someStr.indexOf("a")
instead ofsomeStr.indexOf("a") >= 0
Source: https://www.joezimjs.com/javascript/great-mystery-of-the-tilde/