Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8505483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:19:55+00:00 2026-06-11T02:19:55+00:00

Every so often I run across someone using (!~val) instead of (val === -1)

  • 0

Every so often I run across someone using (!~val) instead of (val === -1) in situations where -1 is returned from a function (e.g. indexOf()).

To me, the logical NOT + bitwise NOT statement seems horribly unreadable when compared to the -1 comparison. Is there enough of a speed difference to warrant using one over the other? Or if not speed, some other reason that I’m missing to use a bitwise NOT instead of === ?

(Aplologies in advance if this is a dup., but I couldn’t find an answer to this exact question. Searching for “!~” doesn’t quite work in SO or Google)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T02:19:57+00:00Added an answer on June 11, 2026 at 2:19 am

    No, they are definitely not the same.

    The bitwise conversion will do an implicit type coercion. The === operator checks for type equality.

    So these these two can give completely different results.

    var val = "-1";
    
    (!~val) // true
    (val === -1)  // false
    

    In a situation like this I think the intent and correctness of the comparison far outweighs any performance consideration. Decide what exactly you want to compare and use the right comparison for the job.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a webapp, and every so often we run into situations where
This is a problem I come across every so often; I always end up
I'm using TextWrangler to edit PHP code. I often run into the problem that
I need a method to run every so often that does some database processing.
Every so often I see Speed Up Your PC programs that offer a RAM
Every now and then (ahem...) my code crashes on some system; quite often, my
I've got a function that often throws an exception (SSH over 3g). I'd like
I have a PHP file that is ran every so often to delete old
I've been working on updating my page using javascript/jquery and have now run into
I find myself using jQuery objects with regular js objects often; I'm trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.