Arrays and Objects in JS :Heterogeneous arrays


#1

var myArray=[23,true,"cnn",add];
var add={
name:"vincent",
age:23,
gender:"woman"};

Hi ,i have two questions:
(1)why myArray contains only :number,string,boolean....several types of data,but
myArray i added the word:add,
it passed the lesson .
the word does not belong to the type of data .why it could pass the lesson ?

(2)i did it before: gender:woman;
of course ,it did not pass the lesson .
i corrected it :gender:"woman"
it passed the lesson .why ?thanks


#2

Hi again @vin5460 :slight_smile:

Variable signatures can't be a data type. ex. var "fun" = some_value (raises a syntax error)

So therefore, the reason why add works without any quotation marks or such is because it's a variable that stores an object in literal notion and you can use that in a heterogeneous array.
Also, for question #2 it works as "woman" because object values must be a data type. Unless you defined woman as a local var somewhere else, it won't work.

I really hope this clarifies things for you!! Have a wonderful day


#3

Thank you for your reply .i continue to study ..thanks


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.