What Does A Tilde Do In JavaScript?
It's not every day that you see a tilde in JavaScript.
I was taking a look through the source code for the Twit NPM library when I was creating a Twitter Bot and came across the following lines of code:
if(~followers.indexOf(target)) {
// do some action
}
Notice the tilde on the first line? Let's find out what that's all about.
I discovered that when we use the ~ operator, it does -(A + 1) where A is the value to the right of the tilde. For example, we'd get -2 if we did this (since -(1 + 1) = -2):
~['apples', 'oranges'].indexOf('apples'); // => 0
~['apples', 'oranges'].indexOf('oranges'); // => -2
Maybe you're like me and you're not really seeing the usefulness of using tilde yet.
It turns out that it's actually useful for determining the truthiness of a numeric value. It flips all the bits in its operand.
For a moment, let's just realize the mad crazy language that we're working with and recall that JavaScript does this:
-2 ? true : false; // => true
0 ? true : false; // => false
-1 ? true : false; // => true 😲
Ah! Isn't that funny? -1 has a truthiness of true and the only integer that has a truthiness of false is 0. So really, we can use tilde as a shorthand. Check this out:
// Instead of doing this
if(!fruits.indexOf('apples') != -1) {
// Mmm, I love apples.
}
else {
// We ain't got no apples
}
// We can do this
// to see if an array contains a particular value
// (btw, there's a better way to do this with ES6 now.
if (!!~fruits.indexOf('apples')) {
// Yay apples
console.log("Found apples!") // <= this will print
}
else {
// Still no apples
console.log("No apples apparently")
}
Now, the question then becomes... am I going to use this? Probably not, but it's cool to know that it exists after having stumbled upon it. I don't think I'd use this much solely because I'd prefer others to understand my code as fast as possible if they have to read it.
I hope you learned something here and won't get alienated next time you see a ~ floating around.
[Update: 2018/02/05] I lied. It's been a single day and I've already used ~ at least 4 times in my personal projects. Hey man, I'm sorry- it's kinda cool!
Discussion
Liked this? Sing it loud and proud 👨🎤.
🚀 55% off The Software Essentialist sale - doors open in 3 days
A lot of devs get stuck in “best practices”.
Between things like SOLID, Clean Code, microservices, single-file components, and so on — there’s a lot you can do...
But in my experience, there’s only a small set of things that really matter at this stage when it comes to making the shift from coding to crafting.
There's only a few things that really move the needle in terms of writing scalable, testable code (on any side of the stack).
That's what the Best Practice-First Phase is all about: the real best practices.
We're doing a 55% off promo of The Software Essentialist from March 31st to April 4th to celebrate the launch of The Best Practice-First phase of craftship in the Early Adopter course.
Folks have been asking me to do one of these for a while, but I wanted to wait until this valuable module dropped.
Join the waitlist for bonuses and to get early bird access (1 to 5 hours before everyone else) when the sale goes live.
Only 200 spots available. Join the waitlist here 🔗.
PS: You can track updates on the course progress & the companion book (solidbook.io) here.
Stay in touch!
Join 15000+ value-creating Software Essentialists getting actionable advice on how to master what matters each week. 🖖
View more in Web Development
You may also enjoy...
A few more related articles