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 and the DOM (Retiring) Traversing the DOM Using previousElementSibling and insertBefore

why not && in the existing if?

I added another condition in the 'if' that checks if the list item is the first child of the parent ul before moving it up. If the list item is the first child, don't do anything.

I noticed in the video another 'if' is written that simply checks for the existence of a previous list item before moving the list item up.

Advantages/disadvantages of the two methods? Or is it simply preference?

2 Answers

Steven Parker
Steven Parker
231,269 Points

:point_right: Expanding the if condition is preferable if the expressions are clear.

In the video, there were 3 lines of code between the first and second if. But assuming your condensation is not to convoluted, that would be a good optimization.

Nested if statements become hard to read and just looks ugly if it becomes too much. Adding multiple logical operators can add complexity to your code especially if there are multiple operators. I personally try to find a balance of the two or try to minimize conditions as much as I can.