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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:45:36+00:00 2026-06-01T09:45:36+00:00

Today I stumbled upon a question here on Stack Overflow – How do I

  • 0

Today I stumbled upon a question here on Stack Overflow – How do I remove objects from a javascript associative array?. What struck me was that the accepted answer was both misleading and heavily upvoted, so I highlighted the possible pitfall.

However, while cobbling together a corrective answer, I realized I have no idea as to why it makes sense for delete to keep elements assign undefined instead of removal.

var elements = new Array()
elements.push(NaN)
elements.push(NaN)
elements.push(NaN)
delete elements[1]
console.log("number of elements: ", elements.length)   // returns 3

Is there a rationale behind it?

  • 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-01T09:45:37+00:00Added an answer on June 1, 2026 at 9:45 am

    I realized I have no idea as to why it makes sense for delete to assign undefined instead of removal.

    It doesn’t. delete removes properties from objects, it does not set them to undefined. Here’s an easy way to tell:

    var a = ['a', 'b', 'c'];
    console.log(1 in a); // logs "true"
    delete a[1];
    console.log(1 in a); // logs "false"
    

    Note that after the delete, a doesn’t have a property called 1 anymore. At all.

    Contrast with:

    var a = ['a', 'b', 'c'];
    console.log(1 in a); // logs "true"
    a[1] = undefined;
    console.log(1 in a); // logs "true"
    

    There, a still has a property called 1, it’s just that the property’s value is undefined.

    It’s useful to understand that in JavaScript, arrays aren’t really arrays at all. They’re just objects, array “indexes” are just property names (which are strings — yes, really, we just tend to write them as numbers), arrays have special handling of property names that are all numeric (indexes), a special length property, and some functions they get from Array.prototype. This is very clearly laid out in Section 15.4 of the spec. Once you have it set firmly in your head that JavaScript arrays aren’t really arrays, they make a lot more sense. 🙂

    Deleting an array “index” property from an array does not change its length (not even if you delete the highest-numbered one); it just creates a hole in the array (JavaScript “arrays” are sparse arrays by their nature; e.g., they can have gaps in them). So in my first example above, I get exactly the same array that I’d’ve gotten if I’d done this:

    var a = [];
    a[0] = 'a';
    a[2] = 'c';
    

    Note the gap, the array has no 1 element/property.

    If you say:

    var foo = a[3];
    

    …foo can get the value undefined for two completely different reasons:

    1. a has a property called 3 that has the value undefined, or:
    2. a has no property called 3 at all; the result of a property accessor operation on an object that doesn’t have a property by that name is undefined. This if-no-property-return-undefined is covered by the spec in a fairly convoluted way, but mostly in Section 8.12.3.

    These are very distinct things.

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

Sidebar

Related Questions

Today I stumbled upon the possibility to access a DOM element in Javascript simply
I stumbled upon this piece of code today: CGRect rect = {{0,0},{w,h}}; Here, I
Today I stumbled upon a rather interesting compiler error: int main() { int const
I've stumbled upon a rather strange issue today with Spring 3.0: There's an abstract
I just today stumbled upon the Code Metrics thing, after having used Visual Studio
Today I stumbled upon an interesting bug I wrote. I have a set of
Today I stumbled upon a case where I would readily assume a simple optimization
I stumbled upon this oddity today while playing with some code to go down
Today I was doing some leisurely reading and stumbled upon Section 5.8 (on page
today I stumbled upon a very interesting case (at least for me). I am

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.