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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:04:58+00:00 2026-06-12T00:04:58+00:00

Possible Duplicate: Are Javascript arrays sparse? I am learning JavaScript at the moment and

  • 0

Possible Duplicate:
Are Javascript arrays sparse?

I am learning JavaScript at the moment and have been reading some simple introductions and tutorials. While looking at the Array object I stumbled upon some details, which strike me as very odd, coming from other languages like C/Java/Scala/…


So lets assume we define an array as such:

var arr = ['foo','bar','qux']

We now assign

arr[5] = 'baz'

which results in our array looking like this:

arr
>> ["foo", "bar", "qux", undefined, undefined, "baz"]

And the length is as expected

arr.length
>> 6

JavaScript has kindly expanded our array to the needed length – six – and the new items are set to undefined – except for the one we actually assigned a value to.

From a low level point of view this is horrible memory-wise. Typically an array would be a continuous range in memory – making an array bigger generally involves copying the whole array to a new memory location, sufficient in size. This is a very costly operation.

Now, I do realize that this is likely not what JavaScript engines are doing, as copying around arrays would be crazy expensive and the memory space would be wasted on all these ‘undefined’ values.

Can someone tell me what actually happens behind the door?

  • Are arrays actually some sort linked lists?
  • Are the ‘undefined’ array items actually there?
  • How expensive is it to work with large arrays that are mostly filled with ‘undefined’?
  • 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-12T00:04:59+00:00Added an answer on June 12, 2026 at 12:04 am

    In the first version of JavaScript, there were no arrays. They were later introduced as a sub-class of that “mother of all objects”: Object. You can test this quite easily by doing this:

    var foo = [1,2,3,4];
    for (var n in foo)
    {//check if n is equal (value and type) to itself, coerced to a number
        console.log(n === +(n) ? 'Number' : 'String');
    }
    

    This will log String, time and time again. Internally, all numeric keys are converted to strings. The Length property merely fetches the highest index, and adds 1 to it. Nothing more. When you display your array, the object is iterated, and for each key, the same rules apply as for any object: first the instance is scanned, then the prototype(s)… so if we alter our code a bit:

    var foo = [1,2,3,4];
    foo[9] = 5;
    for (var n in foo)
    {
        if (foo.hasOwnProperty(n))
        {//check if current key is an array property
            console.log(n === +(n) ? 'Number' : 'String');
        }
    }
    

    You’ll notice the array only has 5 own properties, the undefined keys 4-8 are undefined, because there was no corresponding value found within the instance, nor in any of the underlying prototypes. In short: Arrays aren’t really arrays, but objects that behave similarly.

    As Tim remarked, you can have an array instance with an undefined property that does exist within that object:

    var foo = [1,2,undefined,3];
    console.log(foo[2] === undefined);//true
    console.log(foo[99] === undefined);//true
    

    But again, there is a difference:

    console.log((foo.hasOwnProperty('2') && foo[2] === undefined));//true
    console.log((foo.hasOwnProperty('99') && foo[99] === undefined));//false
    

    RECAP, your three main questions:

    • Arrays are objects, that allow you to reference their properties with numeric instances

    • The undefined values are not there, they’re merely the default return value when JS scans an object and the prototypes and can’t find what you’re looking for: “Sorry, what you ask me is undefined in my book.” is what it says.

    • Working with largely undefined arrays doesn’t affect the size of the object itself, but accessing an undefined key might be very, very marginally slower, because the prototypes have to be scanned, too.

    Update:

    Just quoting the Ecma std:

    15.4 Array Objects
    Array objects give special treatment to a certain class of property names. A property name P (in the form of a
    String value) is an array index if and only if ToString(ToUint32(P)) is equal to P and ToUint32(P) is not equal to
    2^32
    1. A property whose property name is an array index is also called an element. Every Array object has a
    length property whose value is always a nonnegative integer less than 2^32. The value of the length
    property is numerically greater than the name of every property whose name is an array index; whenever a
    property of an Array object is created or changed, other properties are adjusted as necessary to maintain this
    invariant. Specifically, whenever a property is added whose name is an array index, the length property is
    changed, if necessary, to be one more than the numeric value of that array index; and whenever the length
    property is changed, every property whose name is an array index whose value is not smaller than the new
    length is automatically deleted. This constraint applies only to own properties of an Array object and is
    unaffected by length or array index properties that may be inherited from its prototypes.

    An object, O, is said to be sparse if the following algorithm returns true:
    1. Let len be the result of calling the [[Get]] internal method of O with argument “length”.
    2. For each integer i in the range 0≤i

        a. Let elem be the result of calling the [[GetOwnProperty]] internal method of O with argument
           ToString(i).

         b. If elem is undefined, return true.
    3. Return false.

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

Sidebar

Related Questions

Possible Duplicate: JavaScript arrays braces vs brackets I have a simple question that I
Possible Duplicate: Is Chrome's JavaScript console lazy about evaluating arrays? I have the following
Possible Duplicate: Objects vs arrays in Javascript for key/value pairs I have a variable
Possible Duplicate: How to merge two arrays in Javascript Let's suppose I have 2
Possible Duplicate: Are Javascript arrays sparse? Is the following safe in JavaScript? (as in,
Possible Duplicate: JavaScript: formatting number with exactly two decimals Can some one please help
Possible Duplicate: What’s the difference between “Array()” and “[]” while declaring a JavaScript array?
Possible Duplicate: JavaScript replace all / with \ in a string? I have a
Possible Duplicate: Is Chrome's JavaScript console lazy about evaluating arrays? I write next js
Possible Duplicate: How do I enumerate the properties of a javascript object? I have

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.