Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Quickstart Objects and Conditionals Write a Conditional Statement: else

Walo L
Walo L
3,167 Points

Why we need "for" before"if else" ? Can't we just use if else?

EXAMPLE: for (var movie of movies) { if (movie.status === 'available') { console.log("The movie " + movie.title + " plays at " + movie.time);
}
}

Why not: if (movie.status === 'available') { console.log("The movie " + movie.title + " plays at " + movie.time);
}
}

2 Answers

I'm not an expert but I believe you need the for loop to loop through the "properties" inside of the object. Without the for loop you would have to enter each item one by one into the conditional statement. The for loop allows you to loop through the items in the object and apply the conditional statement to each one separately.

Walo L
Walo L
3,167 Points

Got it, thanks :)

Richard Verbraak
Richard Verbraak
7,739 Points

It's using a for Each loop to loop through all the props in the object. It quite literally reads: For Each movie inside of the object movies > checks condition > console.log(whatever info here);

It needs to loop so you can console.log them all in one go instead of manually copying and pasting what you wrote below, for each movie property inside of the object.

Hope this clears it up a bit!

Walo L
Walo L
3,167 Points

Yes that make sense, thanks!