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?
typeof []will return'object'also as there is no'array'type