What ~ does, and is is safe to use?
<p>This will be needed later on: <code>elementStr = 'hello'</code>.</p>

<p>So I am working on an javascript app. In one of my function I have a following if statement: </p>

<pre><code>if (elementStr.indexOf('#') !== 0) {
        //true
    } 
</code></pre>

<p>But I don't like the <code>!==</code> part it doesn't look nice to me. So I tried this:</p>

<pre><code>if (!elementStr.indexOf('#')) {
        //false
    } 
</code></pre>

<p>But indexOf() does not return undefinied but it always returns a number. So the first if statement returns true because <code>elementStr</code> does not have a '#' in the first place of the string. But the second if statement returns false because indexOf returns -1 and thats not 'undefined', but we want it to return true of course like the first example. Because we don't want to change the statement, we just want to make it look better. So by accident I did this:</p>

<pre><code>if (!~elementStr.indexOf('#')) {
    //true
} 
</code></pre>

<p>And gues what, it worked. I tried to find something about that sign, what it does in javascript. But i couldn't find anything. I was playing with it and I got following results: </p>

<pre><code>if (!~~elementStr.indexOf('#')) {
    //false
} 

if (!~~~elementStr.indexOf('#')) {
    //true
} 
</code></pre>

<p>You can go on and add ~ everytime, and the statement boolean will change all the time.</p>

<p>Now can someone explain me what it does in this example, and if I am safe to use that sign?</p>
