Valerii Iatsko

Full Stack Developer

Hi! My name is Valerii Iatsko.

I'm a Full Stack Developer and the author of this blog.

→ Follow on Twitter

Small tip about JavaScript tilde (~)

This will be a very small article just to share the beautifulness of solution using JavaScript tilde.

One can write a simple “counter” program this way:

var repeated = {};
if (repeated[x]) {
  repeated[x]++;
} else {
  repeated[x] = 1;
}

But leveraging the power of tilde, this could be simplified to:

repeated[x] = -~repeated[x];

Essentially, tilde bitwise noting any number, x will become -(x + 1).

Also, I’d recommend to read The Great Mystery of the Tilde(~) article by Joe Zimmerman

© 2017 Valerii Iatsko. Personal opinions.
EN | RU