0


We know JavaScript does not support arrays with named indexes. (Associative arrays)
But when we declare an associative array in javascript, it doesn't throw an error And it works surprisingly.

let arr = [];
arr.name = 'John';
arr.lastName = 'Doe';
arr.age = 46;

let obj = {name: 'John', lastName: 'Doe', age: 46};


When console.log('arr'), it will show:

[name: "John", lastName: "Doe", age: 46] //Shown with a special array sign
   age: 46
   lastName: "Doe"
   name: "John"
   length: 0
   __proto__: Array(0) //Shown Array Not Object


AND:

console.log(arr instanceof Array);  //true
console.log(arr instanceof Object); //true

console.log(obj instanceof Array);  //false
console.log(obj instanceof Object); //true


As you see, Javascript displays an associative array with an array specific sign. [] (bracket)
But it treats as an object with that array Because some array methods and properties (like length) will produce incorrect results.
Also, if JavaScript works with this type of array as an object, why does not use the special mark for the object? {} (curly bracket)
In short, is this a bug in the JavaScript interpreter or a type of ability?

7
  • 1
    Javascript does not have associative arrays, and setting properties of an array object is not "using" the array part. Commented Jun 13, 2019 at 20:01
  • Not a bug.... you are using an array to create an "array like object". typeof [] will return 'object' also as there is no 'array' type Commented Jun 13, 2019 at 20:01
  • 1
    from the standard: "An Array object is an exotic object that gives special treatment to array index property keys ..." Commented Jun 13, 2019 at 20:04
  • 1
    @MR.Mostafa of course it will. Completely irrelevant if it is a populated or empty array. How the console handles output is also irrelevant and will vary depending on environment Commented Jun 13, 2019 at 20:11
  • 1
    You are just adding properties to the Object (see prototype of Array) - this is what would be expected. Commented Jun 13, 2019 at 20:27

2 Answers 2

1

In Javascript, an array is not only "treated as" an object -- it IS an object. As you have already seen, since arr instanceof Object is true.

Therefore, as with all objects, you can assign properties to an array.

length is a special property of arrays, and represents the number of array elements. Since, in your example, arr has no elements, its length property is correctly 0. Objects do not automatically have a special length property.

Sign up to request clarification or add additional context in comments.

Comments

0

The Array is an object - it brings its own methods you can use. The object is not an array - you cant use the methods of the Array-object.

I found this text what describes it with a few words: http://www.javascripter.net/faq/arrayobject.htm

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.