Array question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Array question

This is a question from a timed JavaScript Challenge. What is the output? var e = []; e["Yes"] = 7; console.log(e["Yes"]); e[null] = 8; console.log(e[null]); e[true] = 3; console.log(e[true]); e[false] = 2; console.log(e[false]); The output is 7832. Is this an example of non-integer indices? Through Google I found this in Stack Overflow: " JS arrays do not have key indices, those JS objects are considered object literals. so: foo["bar"] = 1; foo.bar === foo["bar"]; //true foo["bar"] === foo[0]; //false This is one of the many subtle quirks about JS that throw people off easily. " Am I connecting the correct concept with this challenge question? Thank you in advance!

1st Nov 2018, 1:10 AM
DumbledoresAmy
DumbledoresAmy - avatar
3 Answers
+ 4
Yes ~ and sorry for the late response It creates an object that contains one property. That property has a key name of foo, and that key called foo contains a vaue of "bar" If you assign the object to a variable, such as baz, you can retrieve the property value by using either array-index notation, or by accessing the appropriate property of that object.
1st Nov 2018, 6:14 PM
BroFar
BroFar - avatar
+ 2
BroFarOps 🎃👻🍬 Could I pick your brain?
1st Nov 2018, 4:38 PM
DumbledoresAmy
DumbledoresAmy - avatar
+ 2
That's so cool, thanks!
1st Nov 2018, 6:59 PM
DumbledoresAmy
DumbledoresAmy - avatar