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 991473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:06:50+00:00 2026-05-16T06:06:50+00:00

While debugging a javascript code that uses jQuery I found the following code: [0,

  • 0

While debugging a javascript code that uses jQuery I found the following code:

[0, 0].sort(function()
{
    baseHasDuplicate = false;
    return 0;
});

By my understanding of javascript this code will sort array containing two zeroes with comparison function that will always set a global variable and will return equality, which has same effect as baseHasDuplicate = false;.
Coming from a valued source I think I missed something.
Did I miss something or is this a programming fail?

  • 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-05-16T06:06:51+00:00Added an answer on May 16, 2026 at 6:06 am

    As you can see here (chinese), this code might be used to test for Chrome. EDIT: see below for the complete story..

    As explained in the article, what happens is that Chrome optimizes the “.sort(…)” method in such a way that the [0, 0].sort(...) call won’t execute the given comparison function.

    From the article, Chrome’s implementation of “.sort(…)” is something like:

    function sort(comparefn) {
      var custom_compare = (typeof(comparefn) === 'function');
      function Compare(x,y) {
        if (x === y) return 0;
        if (custom_compare) {
          return comparefn.call(null, x, y);
        }
        ...
    }
    

    As 0 === 0 is true, it won’t call comparefn.

    In the case of jQuery, it won’t set the global variable baseHasDuplicate to false.


    EDIT: if you browse Sizzle’s source code, here for example (go to the yellow section under “Sizzle CSS Selector Engine”, called “Sizzle variables”), you will find the following explanation:

    var chunker = /((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^[\]]*\]|['"][^'"]*['"]|[^[\]'"]+)+\]|\\.|[^ >+~,(\[\\]+)+|[>+~])(\s*,\s*)?((?:.|\r|\n)*)/g,
            done = 0,
            toString = Object.prototype.toString,
            hasDuplicate = false,
            baseHasDuplicate = true;
    
    // Here we check if the JavaScript engine is using some sort of
    // optimization where it does not always call our comparision
    // function. If that is the case, discard the hasDuplicate value.
    //   Thus far that includes Google Chrome.
    [0, 0].sort(function(){
            baseHasDuplicate = false;
            return 0;
    });     
    

    Looks demystified!

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

Sidebar

Related Questions

While debugging javascript written by someone else, I came across some code that I've
While debugging jQuery apps that use AJAX, I often have the need to see
I'm debugging and upgrading some legacy PHP and JavaScript code and I've found some
I'm trying to write a simple Javascript(jQuery) function that randomly displays 6 Divs out
While debugging some client side javascript today in Firefox, I ran into something that
I noticed while debugging some Javascript that an extra argument that I was passing
While debugging and keep pressing F5, if the source code does not exist, eclipse
While debugging an issue with our system, I have discovered a thread contention that
While debugging a problem in a WPF application, I have noticed that the TwoWay
i get this dialog while debugging code in visual studio 2008. (your step-into request

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.