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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:07:19+00:00 2026-06-12T01:07:19+00:00

I was confusing myself a little with a thought experiment and now I’m looking

  • 0

I was confusing myself a little with a thought experiment and now I’m looking for some advice. Its about ECMAscript references and the Array.prototype.indexOf() method.

Lets start easy:

var container = [ ];
// more code
container.push( 5 );
container.push( 7 );
container.push( 10 );

So now we pushed some “primitive values” into our ECMAscript array (whether or not that statement is true I’ll come back for), at least I imagined it like this so far. A call to

container.indexOf( 7 );

will return 1 as expected. The big question I’m having is, if .indexOf() really compares the primitive value or if in reality a Number() object is created + stored and its reference is getting compared. This becomes a little more obvious if we re-write that like so:

var a = 5,
    b = 7,
    c = 10;

var container = [ ];

container.push( a );
container.push( b );
container.push( c );

container.indexOf( b );

Until this point, one could still easily argue that all .indexOf() needs to do is to compare values, but now lets look at something like this:

var a = { name: 'a', value: 5 },
    b = { name: 'b', value: 10 },
    c = { name: 'c', value: 15 };

var container = [ ];
// more code
container.push( a );
container.push( b );
container.push( c );

Here, we filled that container array with object-references and still, .indexOf() works as expected

container.indexOf( b ) // === 1

while a call like this

container.indexOf({ name: 'b', value: 10 });

obviously returns -1 since we are creating a new object and get a new reference. So here it must internally compare references with each other, right?

Can some ECMAscript spec genius confirm that or even better link me some material about that ?

A side question on this would be if there is any possibly way to access an internally stored object-reference within a lexicalEnvironment respectively Activation Object.

  • 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-12T01:07:20+00:00Added an answer on June 12, 2026 at 1:07 am

    It boils down to indexOf() comparing against each array property in turn using the same algorithm as the === operator.

    The relevant section of the ECMAScript 5 spec is section 15.4.4.14, step 9, section b (highlighting mine):

    If kPresent is true, then

    i. Let elementK be the result of calling the [[Get]] internal method of O with the argument ToString(k).

    ii. Let same be the result of applying the Strict Equality Comparison Algorithm to searchElement and elementK.

    iii. If same is true, return k.

    References:

    • indexOf()
    • Strict Equality Comparison Algorithm
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On a regular basis I find myself writing little utility programs that use some
Confusing title, hopefully some code will clarify: struct MyNestedType { void func(); }; struct
Does that title sound confusing? I thought it might well anyways. When I person
I'm confusing myself terribly grasping the concept of plotting on a 3D plane, if
Let me know if I'm not explaining this well enough as I'm confusing myself
Maybe Im just confusing myself due to being pressed for time, but how would
As an RoR newbie, I greatly appreciate any/all assistance in advance! I'm confusing myself
Confusing myself with this OpenGL stuff. What I want to do is move a
I'm confusing myself reading Microsoft's documentation on the Lazy<T>(bool) constructor. The parameter is described
I am to the point I am confusing myself but here is what I

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.