Confused Array!!!!

Without much ado, let’s get to the code directly

var obj = [];

obj[ "a" ] = 10;
obj[ "b" ] = 20;
obj[ "c" ] = 30; 

console.log( obj.length );  // returns ZERO!!!

Experienced JS programmers would have seen the similar type of code, where in Array are being used as plain JS object and their response would just be – let’s fix this now!

The problem is the mixed usage which is possible cause of dynamic nature of the language. The usage in such a way does not cause any loss of data and you can just iterate it like a plain JS object as

for( var name in obj ){
   if( obj.hasOwnProperty( name ) ) {
      console.log( obj[name] );
   }
}

// following works too.. 
console.log( obj["a"] );

Any usage with respect to Array API would do nothing such as

    console.log( "Array Length => "  + obj.length ); //returns ZERO, since array has no elements. "a"/"b"/"c" are properties of array object! 
    console.log( obj[1] ); //returns undefined, no element in array! 

However in that case, why would choose to say that “obj” is any Array! It is not…