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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:12:15+00:00 2026-05-11T02:12:15+00:00

I have been playing with javascript arrays and I have run into, what I

  • 0

I have been playing with javascript arrays and I have run into, what I feel, are some inconsistencies, I hope someone can explain them for me.

Lets start with this:

 var myArray = [1, 2, 3, 4, 5]; document.write('Length: ' + myArray.length + '<br />'); for( var i in myArray){    document.write( 'myArray[' + i + '] = ' + myArray[i] + '<br />'); } document.write(myArray.join(', ') + '<br /><br />'); 
 Length: 5 myArray[0] = 1 myArray[1] = 2 myArray[2] = 3 myArray[3] = 4 myArray[4] = 5 1, 2, 3, 4, 5 

There is nothing special about this code, but I understand that a javascript array is an object, so properities may be add to the array, the way these properities are added to an array seems inconsistent to me.

Before continuing, let me note how string values are to be converted to number values in javascript.

  • Nonempty string -> Numeric value of string or NaN

  • Empty string -> 0

So since a javascript array is an object the following is legal:

 myArray['someThing'] = 'someThing'; myArray[''] = 'Empty String'; myArray['4'] = 'four';

for( var i in myArray){ document.write( 'myArray[' + i + '] = ' + myArray[i] + '<br />'); } document.write(myArray.join(', ') + '<br /><br />');

 Length: 5 myArray[0] = 1 myArray[1] = 2 myArray[2] = 3 myArray[3] = 4 myArray[4] = four myArray[someThing] = someThing myArray[] = Empty String 1, 2, 3, 4, four 

The output is unexpected.

The non empty string '4' is converted into its numeric value when setting the property myArray['4'], this seems right. However the empty string '' is not converted into its numeric value, 0, it is treated as an empty string. Also the non empty string 'something' is not converted to its numeric value, NaN, it is treated as a string. So which is it? is the statement inside myArray[] in numeric or string context?

Also, why are the two, non numeric, properities of myArray not included in myArray.length and myArray.join(', ')?

  • 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. 2026-05-11T02:12:16+00:00Added an answer on May 11, 2026 at 2:12 am

    The keys of a JavaScript array are actually strings. For details and an implementation of a map type for arbitrary keys, check this answer.


    To clarify and add to what Jason posted: JavaScript arrays are objects. Objects have properties. A property name is a string value. Therefore, array indices are converted to strings as well before anything more can happen. A property name P will be treated as an array index (ie the special array-magic will be invoked) if the following holds (ECMA-262, 15.4):

    ToString(ToUint32(P)) is equal to P and ToUint32(P) is not equal to 2^32 − 1

    That numeric indices will be converted to strings (and not the other way around) can be easily verified:

    var array = []; array[1] = 'foo'; array['1'] = 'bar'; array['+1'] = 'baz'; document.writeln(array[1]); // outputs bar 

    Also, its bad practice to iterate over an array’s entries with a for..in loop – you might get unexpected results if someone messed with some prototypes (and it’s not really fast, either). Use the standard for(var i= 0; i < array.length; ++i) instead.

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

Sidebar

Related Questions

I have been playing with the Ruby library shoes. Basically you can write a
I have been playing around with the EF to see what it can handle.
New to SO and javascript. I have been playing with JQuery for a couple
I have been playing around JavaScript for a year or so, one thing I
Hopefully someone can help, I've been playing with ASP.NET MVC 2 for a learning
I have been playing with this for a while, but the closest I have
I have been playing with the Linq to Sql and I was wondering if
I have been playing around with the postgresql.conf file for a couple days now.
I have been playing with the demo code from this msdn article by Jeffrey
I have been researching and playing with functional programming lately, solely to broaden my

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.