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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:55:51+00:00 2026-06-13T09:55:51+00:00

Does anyone why is the otherwise excellent jQuery.each function is designed differently from the

  • 0

Does anyone why is the otherwise excellent jQuery.each function is designed differently from the (now) native Array.forEach? F.ex:

var arr = ['abc','def'];
arr.forEach(function(entry, index) {
    console.log(entry); // abc / def
});

This makes absolute sense. But jQuery chose to put the index as first argument:

$.each(arr, function(index, entry) {
   console.log(entry);
});

Does anyone know the reasoning behind this design decision? I have always used $.each extensively, but it always bugged me that the index was the first argument as it is rarely used. I know jQuery implemented a direct reference through this but it’s very confusing if you do:

​var arr = ['abc','def'];
$.each(arr, function() {
    console.log(this === 'abc'); // false both times, since this is a String constructor
});​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

Not that it bothers me so much, I prefer to use native polyfills for the most common new array functions, but I have always been curious about the design decision. Maybe it was made in older times before browsers implemented native forEach and legacy support prevented them from changing it, or…?

Or maybe, it is designed this way because is can be used on native objects too, than then it “makes sense” to put the key before value in the callback…?

Sidenote: I know underscore.js (and maybe other libraries) does it the other way around (more similar to the native function).

  • 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-13T09:55:52+00:00Added an answer on June 13, 2026 at 9:55 am

    Well, I guess we’d have to ask Mr. Resig himself for an explanation on this. Fact is, that ECMAscript 262 edition 5 wasn’t very widespread the time jQuery was designed and developed, so this definitely comes into play. And since it was designed like so, they didn’t want to change it later and break all existing code.

    In fact, its much more likely that you want to access an element with a higher priority, than the index of it when looping an Array. So, to me there is no reasonable explanation why you would pass in the index first into the callbacks.

    Be assured, if jQuery was invented today, they would follow the native implementation behavior.

    On the other hand, if it bugs you too much you can simply create a shortcut and use the native Array.prototype.forEach to iterate your jQuery wrapped sets:

    var forEach = Function.prototype.call.bind( Array.prototype.forEach );
    
    forEach( $('div'), function( node ) {
        console.log( node );
    });
    

    ..and for standard Arrays, just go with their native prototype.

    while implementation conditional return false/true,we must know what part work in which manner. When you use return false with condition in Array.prototype.forEach it treated as continue, but When you use return false, with condition in $.each it treated as break statement.

    var listArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
    var arr1 =[];var arr2=[];
    var rv = true;
    listArray.forEach(function(i, item) {
      if (i == 5) {
        return rv = false;
      }
     arr1.push(i)
      return rv;
    });
    var listArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
    jQuery.each(listArray, function(i, item) {
      if (item == 5) {
        return rv = false;
      }
      arr2.push(i)
    });
      console.log("forEach=>"+arr1)
      console.log("$.each=>"+arr2)
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Folks, Please does anyone know how to show a Form from an otherwise invisible
Does anyone have a translate function for x/y positions after rotation in javascript? for
Does anyone know how to, given an mp3 file, remove the id3v2 header from
does anyone know how to prevent hibernate from loading a collection or many-to-one assocation?
Does anyone have any idea how to retrieve deleted records data from Apex data
Does anyone know of any issues (performance or otherwise) associated with a FILEGROWTH setting
I've just updated Ubuntu and then Eclipse crashed. Does anyone know a solution? Otherwise
Does anyone know of a library -- open source or otherwise -- that I
Does anyone know of any work (academic or otherwise) that thoroughly compares the ability
Does anyone know of an application (hosted or otherwise) that I could use 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.