DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. JavaScript
  4. JavaScript's Tilde: Its Real Use Is No Mystery

JavaScript's Tilde: Its Real Use Is No Mystery

A web developer quickly reviews the function of the tilde in JavaScript, and how its used by the computer to convert numbers to binary.

Joseph Zimmerman user avatar by
Joseph Zimmerman
·
Aug. 13, 18 · Analysis
Like (2)
Save
Tweet
Share
4.23K Views

Join the DZone community and get the full member experience.

Join For Free

A while back I wrote an article titled "The Great Mystery of the Tilde" where I tried to explain what the ~ (tilde) operator did in JavaScript. When trying to demystify what it was, I ended up perpetuating a lot of misinformation. I'm here to fix that today.

Setting the Facts Straight

Normally, I don't care too much about going back and updating old content, but my error was quite egregious. Not only that: that post is the second most view post I had last year, so if I'm going to have that much traffic going to it, then I need to fix it, so I've added a paragraph to that post directing visitors to this new post, so that I can prevent future misinformation from spreading.

So, What Is It?

The ~ operator in JavaScript is the Bitwise NOT operator. If you don't work with binary very often, then that probably doesn't mean anything to you at all. Don't worry, though, I'll explain. If you have a number, say 8, it looks like this in binary: 1000. When you use the ~ on that number, it will convert each bit to the opposite value. That means, each 1 will become a 0 and vice versa. So ~8 would equal 7, whose binary representation looks like this: 0111. Or a 13 ( 1101) would become 2 ( 0010). The only problem is that JavaScript uses what is called "Two's Complement" in order to be able to represent positive and negative numbers in binary. So, because of this, in actuality, ~8 equals -9. If you want a better explanation than that, you'll need to do some research on your own because I don't feel adequate to explain it.

If you're wondering what you would use this for (besides as a lame hack as I recommended in my previous article and that I no longer recommend)... so am I. I can find some examples on the internet for using bitwise operators, but I haven't really seen a helpful example utilizing this particular operator. So, if you find a tilde in some JavaScript code you're reading, then check out my old article linked above just to make sure they're not using it wrong. If it doesn't seem to be used hackily, then I guess you'll have to let me know so I can see an example where it's used correctly!

Conclusion

So now I hope this sets the record straight and that people being directed to the old post by Google will end up seeing this post. God bless and happy coding!

JavaScript

Published at DZone with permission of Joseph Zimmerman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Stop Using Spring Profiles Per Environment
  • Distributed Tracing: A Full Guide
  • File Uploads for the Web (2): Upload Files With JavaScript
  • Fixing Bottlenecks in Your Microservices App Flows

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: