The Great Mystery of the Tilde(~)

When was the last time you typed in a tilde? Did you even know that little squiggle on the top left corner of your keyboard is called a tilde? More importantly to this blog, do you know what the tilde does in JavaScript? Probably not, but you’ll soon find out.

The tilde is an operator that does something that you’d normally think wouldn’t have any purpose. It is a unary operator that takes the expression to its right performs this small algorithm on it (where N is the expression to the right of the tilde): -(N+1). See below for some samples.

1
2
3
4
5
console.log(~-2); // 1
console.log(~-1); // 0
console.log(~0); // -1
console.log(~1); // -2
console.log(~2); // -3

So, unless you actually have an application that needs to run this algorithm on numbers – which I doubt very many people do – how are we going to use this squiggly little character to our advantage?

Converting Strings to Numbers

The tilde character needs a numeric value for N, so if you place it in front of a string expression, it will convert that string to a number for the calculation. That still leaves us with a not-so-helpful number because it doesn’t match the number that was in the string. If you’re any good at mathematics, though, you’ll realize that if you run this algorithm twice, you’ll get the same number that you started with. With a string, it’ll convert it to a number, then change it, then change it back to the number.

1
2
3
4
5
6
7
8
console.log(~~-1);    // -1
console.log(~~0); // 0
console.log(~~1); // 1
console.log(~~"-1"); // -1
console.log(~~"0"); // 0
console.log(~~"1"); // 1
console.log(~~true); // 1
console.log(~~false); // 0

As shown in the above code example, you can also convert true and false into 1 and 0 respectively. I don’t see quite as many uses for this, but I wouldn’t doubt that there were some. Also, I realize that there are plenty of ways to convert strings into numbers and I’m not claiming this is the best way. I’m just saying that this is a viable option.

~-1 is 0

If you look back at the first code sample, you’ll see that using ~ on -1 converts it to 0. The number 0 is a falsey value, meaning that it will evaluate to false when converted to a Boolean. That might not seem like a big insight at first, but remember functions like indexOf will return -1 when the query is not found. This means that instead of writing something similar to this:

1
2
3
4
5
if (someStr.indexOf("a") >= 0) {
// Found it
} else {
// Not Found
}

You can now have fewer characters in your code so you can write it like this:

1
2
3
4
5
if (~someStr.indexOf("a")) {
// Found it
} else {
// Not Found
}

I think this might be a more common use case, and probably the only way I’ll actually use tilde unless I run into a situation where other string conversions are hard to read within the context they’re being used (such as here: num = 1 + +"2").

ADDED 3/6/2012: One thing you need to consider before using the tilde as a means to convert strings to numbers (as pointed out in the comments by Beej Jorgensen) is that it will convert into an integer, not a decimal number. “3.14” and 3.14 will both end up as just 3 (with double tildes). Also somewhat important to note: ~ is a Binary NOT operator, which means that it doesn’t actually run the algorithm stated above. The algorithm above is just one way for you to be able to understand what the outcome will be when this operator is applied to it (Thanks Danny Ayers and Anonymous for pointing this out).

Reining In the Tilde

So, did you actually know what the tilde operator did? Or that it even existed? I know I didn’t until recently, and I probably would have gotten along just fine without ever learning about it, but it’s always fun to know something that most others don’t know, you know? If you can think of anything other fun things you can do with the tilde, let me know in the comments below! Also, let the rest of the JavaScript programming community in on this little-known operator by sharing this post with your friends via the buttons below. As always: Happy Coding!

Author: Joe Zimmerman

Author: Joe Zimmerman Joe Zimmerman has been doing web development ever since he found an HTML book on his dad's shelf when he was 12. Since then, JavaScript has grown in popularity and he has become passionate about it. He also loves to teach others though his blog and other popular blogs. When he's not writing code, he's spending time with his wife and children and leading them in God's Word.