JavaScript array and object in single variable?

raj

I recently came across some code which I am a little confused about. Below is similar code, have a look:

var x = [];
console.log(x); // prints [] (no surprise)
x[0] = "abc";
console.log(x); // prints ["abc"] (no surprise)
x["test"] = "testing"
console.log(x); // prints ["abc"] 
console.log(x.test); // prints "testing"

so in this case.. the same variable, x, is both array and an object. How is this possible? If it acts as both, then console.log(x) should print something like ["abc",test:"testing"] but that syntax is wrong.

So whats happening in this case?

thefourtheye

All Arrays are Objects, (not only them, everything in JavaScript is an Object (except the primitives though))

console.log([] instanceof Object);
// true

just that they are special kind of objects which process integer keys differently.

So, you can expect them to act like normal JavaScript Objects. You can add arbitrary properties to them.

About the console.log result,

[ 'abc', test: 'testing' ]

is just a representation given by the implementation. If the representation has a key : value format, then it is a normal property associated with that array. That's all.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Single colon in JavaScript as a prefix to a variable (not object literal)

Set variable to an object array with javascript

PHP object array to javascript variable

Javascript variable not set in array object

combine multiple array elements into single object in javascript

Combine Array(s) into single object in JavaScript

Flatten array into single dynamic object using Javascript

combining and filtering array of objects into a single object in javaScript

How to convert an object which can be array or single object to an array in javascript

convert single value array within a nested object to a single value javascript

Aggregate array of object to single distinct value of object javascript

Update a Single Field of Object in Object Array in Javascript/Lodash

object array to single array

Change array variable inside Javascript object literals

Storing a variable in an array (that functions as a value in an object) JavaScript

Join an array object and string variable in javascript or angularjs

Accessing a variable from an object in an array in JavaScript?

Variable keeps turning into an object instead of an Array in Javascript

How to access object array using javascript with variable

Javascript remove object from array using object variable

How to check emptiness with single condition regardless of input is object or array in Javascript

Remove a single instance of object dulplicates in an array inside JavaScript

How to check same property exists in single array of object javascript

How to reduce my object array into a single depth in javascript?

Accessing a single value for specific key pair from object in a Javascript array

Turn an array into a single object

Add a single object to an array

return a single object in an object array

Javascript compare results in a single object array and append if a value matches within the same object array