DEV Community

Discussion on: Can You Guess the Result of This JavaScript Code?

Collapse
 
firecrow8 profile image
Firecrow Silvernight • Edited

I think it would be c but lets make it a little more interesting

const arrOrObj = [];
arrOrObj[1] = "a"
arrOrObj["1"] = "b"

// What is this?
console.log(arrOrObj["1"]);
Enter fullscreen mode Exit fullscreen mode

Array indexe of 1 auto populates an array of length 2, does the string "1" go in the index or the object key value store?

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

Did you mean to leave x unused, or is that a mistake in your example?

Either way, after line 3 the array [undefined, "b"] so whether you index it with ["1"] or with [x], the result is going to be b

Collapse
 
firecrow8 profile image
Firecrow Silvernight

Your right that x was unused, I've removed that. That's true, the reason I posted this is because arrays are slightly different from object bracket access.

but yes it's b