THE DYSLEXIC DEVELOPER

Quick Tip - The Tilde Operator

July 12, 2018

QUICK TIP TIME!!!!!

This:

const str = 'awesome';
if (str.indexOf('a') !== -1) {
    console.log('PASS');
}
//PASS

Is the same as this:

const str = 'awesome';
if (~str.indexOf('a')) {
    console.log('PASS');
}
//PASS

WUT!

This is an example of the bitwise NOT operator. You can read more on what Bitwise Operators but this is one of the them.

The first code example is a pre ES6 example of checking if a string contained a sub string. This has been replaced with the String.includes prototype.

The ~ (tilde) operator preforms a NOT operation it which inverts the bits and returns the opposite value.

When the NOT operator is run on a number it returns -(x + 1) so for example when NOT is run on 0 it returns -1. The main reason to use tilde is to handle the 0 index being returned and not needing to check the value against -1.

I understand!

Should you use this?

NO! this does look cool in your code, but it can be hard to understand. If you had never heard of the operator before you read this post. Then you would struggle when you found ~ in some code while fixing a production bug at 5am.