Accessing arrays that are object properties

Hi all,

I’m starting the Mixed Messages project and I’m stuck. I assigned 3 variables for 3 arrays with my strings in that I’ll later pick out at random, but I’m struggling to access the array’s length property.

So far I have…

const arrays = {
‘countries’ : [‘England’, ‘Italy’, ‘France’],
‘capitals’ : [‘London’, ‘Rome’, ‘Paris’],
‘leaders’ : [‘Boris Johnson’, ‘Sergio Mattarella’, ‘Emmanuel Macron’]

let randomCountry = arrays.countries()

arrays.countries.length() won’t work obviously. I’m not sure what to do and can’t find any other resources online. Any help/advice would be appreciated. I’m new to coding and struggle with JavaScript a little.


Hey Ben how are you doing?

If you paste your code inside a Codebyte is easier to help you out. Remember it for next time :wink:

So here’s your code:

const arrays = { 'countries' : ['England', 'Italy', 'France'], 'capitals' : ['London', 'Rome', 'Paris'], 'leaders' : ['Boris Johnson', 'Sergio Mattarella', 'Emmanuel Macron'] };

The object declaration is fine and we can print anything we want:

To access any single item you do it with the square brackets notation:

This line, is not correct unfortunately as countries is an array not a function:

let randomCountry = arrays.countries()

Hi! All good thanks! Thank you for replying.

That’s great, thank you. How would I go about accessing the length of each array? If the array was outside the object I could simply do say countries.length() could I not? The tip said to include all arrays in a single object so it’s thrown me a little. I think I may take them out of the object…

Yes. If the array is the property of an object, you simply have to include the object in your reference.


The syntax would look like:

const myObject = {
  'array1': [1, 2, 3, 4],
  'array2': [5, 6, 7, 8, 9]

1 Like

That’s done the trick! Thank you so much. I get the output I expected, but I also get undefined, so I’ll go back and look again but at least I got what I wanted out of it in the end. :slight_smile:

1 Like

That’s not an indication of a problem per se. All functions in JavaScript return a value. If a function lacks an explicit return statement, it implicitly returns undefined. If you have a function that uses console.log() to print something to the console, and call that function as an argument passed to console.log(), the function you called will print to the console, return undefined to the caller which is a console.log() function, so that returned undefined gets printed to the console as well.

const someFunction = param => console.log(param); // simple function that prints what is passed to it // calling the function as an argument to console.log console.log(someFunction("Hello")); // simply calling the function someFunction("Notice that undefined is not printed on the following line.");
1 Like

Thanks! I changed the console.log within the function randomMsg itself to a return statement, then reran console.log(randomMsg()); and it worked.

const randomMsg = () => { return `${capital} is the capital city of ${country}. The head of state there is ${leader}!` }; console.log(randomMsg());

Thank you. :slight_smile:

1 Like