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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:53:15+00:00 2026-06-09T15:53:15+00:00

This question actually is an outcome from another question , for which i have

  • 0

This question actually is an outcome from another question, for which i have made some experiments, results are confused me more. I’m expecting answers which explains what actually happens internally.

What i have tried is,

I kept this as the base assumption because i got some clear explanation
here,

var a = [];
a['a1'] = [];
a['a2'] = [];

console.log(a); // []
console.log(a['a1']); // []
console.log(a['a2']); // []

TEST 1

This confused me a lot, since it prints b[0] as a and can be able to access length property, i thought var b can be treated as a character array, therefore i tried to assign another value , but ended up without success. From the base assumption, if this one can be treated as a char array(more generally as an array), the assignment should have been successful. It breaks the base assumption.

var b = 'abcd';
b['b1'] = [];

console.log(b); // abcd
console.log(b.length); // 4
console.log(b['b1']); // undefined

TEST 2

But if i create like this, the assignments are happens,

var bb = ['a', 'b', 'c', 'd'];
bb[4] = [];

console.log(bb); // ["a", "b", "c", "d", []]
console.log(bb.length); // 4
console.log(bb[0]); // a
console.log(bb[4]); // []

From this, i thought that, b[4] = []; may be successful, but

var b = 'abcd';
b[4] = [];

console.log(b); // abcd
console.log(b.length); // 4
console.log(b[4]); // undefined

My question is, why these assignments behaving differently while the variables sharing some common functionalities?

Here is the demo

Can anyone please give me a clear cut explanation about what actually happening internally?

Extra tests

Then if i tried with numerical assignment, it behaves quite differently form those two.

var c = 1234;
c[4] = [];

console.log(c); //1234
console.log(c.length); // undefined
console.log(c[0]); //undefined
console.log(c[4]); //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-09T15:53:16+00:00Added an answer on June 9, 2026 at 3:53 pm

    Reasoning

    Your “Base Assumption” works because a is an array. “Test 2” is the only other test case that you have written that uses an array, which is why that is the only other test case that works.

    You seem to be assuming that providing a square-bracket notation and having a length method indicates that an object is an array. This is not true. To test to see if an object is an array, you can use JavaScript’s instanceof method, as follows:

    var a = [];
    var b = 'abcd';
    var bb = ['a', 'b', 'c', 'd'];
    var c = 1234;
    
    a  instanceof Array // => true
    b  instanceof Array // => false
    bb instanceof Array // => true
    c  instanceof Array // => false
    

    Notice that the cases where instanceof Array returns true are the ones that are acting as you expected, since you are trying to perform array operations.


    Why “Test 1” Failed

    For “Test 1”, square-bracket notation for strings performs the string class’s charAt function. Given b = 'abcd', performing b[0] is the same as performing b.charAt(0). It is read-only and simply returns the character at that index, which is “a”.

    See the Mozilla Developer Network documentation for Strings for more details.


    Why “Extra Test” Failed

    For your “Extra Test”, integers do not provide a square-bracket notation or a length method, and thus all of those calls failed.

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

Sidebar

Related Questions

This question has actually came from my experiments with GWT framework but I decided
This question is actually inspired from another one here on SO and I wanted
I have a question which arose from another question about precision of floating numbers.
This question is more about guidance than actually solving my problem: I need to
I am finally asking this question as I have never actually found an answer.
This is actually a three-part question, which I'll explain below, but the questions are:
OK, this question is actually a follow-up question from my previous one: What would
Actually this question is based on a Blog Entry , which discusses the topic
I have actually seen this question quite a bit here, but none of them
Problem This question actually came up at work today. We are planning an experiment

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.