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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:25:57+00:00 2026-06-16T23:25:57+00:00

A strange question, but every time I use jQuery.each() I wonder why the indexInArray

  • 0

A strange question, but every time I use jQuery.each() I wonder why the indexInArray is the first argument and not the second (the second currently being the value itself), like the native array.forEach()?

There is an identical, but unanswered question with a decent number of upvotes on the API docs for .each() too!

More often than not, I find code that “acts on” the value and not the index, resulting in an unused variable in the body of the function (which makes js(h|l)int complain, with good reason in my opinion). So, why isn’t the value the first argument?

Is this a hangover from an poorly thought out implementation in an earlier version, or is there a good reason for this?

  • 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-16T23:25:59+00:00Added an answer on June 16, 2026 at 11:25 pm

    The reason is that the first argument of foreach, the value, is less needed in $.each as jQuery provides it as this.

    Thus, the index appears to be more often useful. This is a good reason.

    I write “appears”, because this doesn’t come without perils : this isn’t exactly the same as the value when the value is of primitive type : in this case the value is wrapped as an object.

    Why is it dangerous ?

    Here’s why :

    [1, 2, 3].forEach(function(v) {
        if (v===2) console.log('found!');
    });
    

    Output in the console :

    found!
    

    Now with jQuery :

    $.each([1, 2, 3], function() {
        if (this===2) console.log('found!');
    });
    

    What’s the output ?

    Nothing.

    (note that I detail the implementation leading to this in this related question.)

    Today, with people being more and more used to the standard javascript function, it might be thought that having the other order would have been better (that was the choice of Underscore.js). The reason to provide the index first was good, but maybe not good enough. But maybe it doesn’t really matter as we probably won’t continue using $.each now that foreach is generally available (at least for arrays).

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

Sidebar

Related Questions

This might seam like a strange question but.... public string MyProperty { get {
Bit of a strange question here i know. but i wanted to know if
My question might sound rather strange, but still I didn't find any reference and
Strange, but I can't find a duplicate for this question. I have a superclass
I've got a question that might sound strange. We are using node.js, but the
I know it's a simple question, but I have strange behaviour in here. <pre>
So, the question might seem strange, but let's say that I compile: int main(void)
my question is a little bit strange, but how is it possible to read
This is a moot question as I'm not on this project any more, but
This might sound kinda strange question but I am puzzled. I was just looking

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.