How is logging an array to console, the entire process, different than a string?


#1
let condiments = ['Ketchup', 'Mustard', 'Soy Sauce', 'Sriracha'];

const utensils = ['Fork', 'Knife', 'Chopsticks', 'Spork'];

condiments.push('bbq','Secret Family Recipe');

console.log(condiments);

Here is the exercise: Arrays

For some reason actually logging to console is not working with the same strategy as just using a string:

The below is an example of a string:

newYearsResolutions.push('eat good food', 'laugh!');

#2

I think its because newYearsResolutions is undefined??? let newYears = [] ///// newYears.push();


#3

I answered my own question:grin:


#4

Two strings, actually. When pushed to the array they will be separate elements.

When we print an array, such as,

console.log(condiments)

what gets printed is a string representation of the array and its structure…

[ 'Ketchup', 'Mustard', 'Soy Sauce', 'Sriracha' ]

We can also extract the elements in a single string…

console.log(condiments.toString())

Ketchup,Mustard,Soy Sauce,Sriracha

Ideally, logging an array is done by iterating over the elements and printing each one, in turn.

condiments.forEach(x => console.log(x))

Ketchup
Mustard
Soy Sauce
Sriracha

Not sure if this addresses your question. Please elaborate further if not.


#5

I was able to answer through trial and error but it looked like you use the same console.log process.


#6

i was wrong lol newYears = [‘La’, ‘La’];

newYears.push(‘lo’);
console.log(newYears);


#7

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