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 Solution: Using nextElementSibling

Question : if (prevLi) {ul.insertBefore(li, prevLi);} and if (nextLi) {ul.insertBefore(nextLi, li)} .

What do the preLi and nextLi works in the if argument "if (prevLi) & if (nextLi) " here?

if (event.target.className == 'up') { let li = event.target.parentNode; let prevLi = li.previousElementSibling; let ul = li.parentNode; if (prevLi) { ul.insertBefore(li, prevLi); } }
if (event.target.className == 'down') { let li = event.target.parentNode; let nextLi = li.nextElementSibling; let ul = li.parentNode; if (nextLi) { ul.insertBefore(nextLi, li); }

1 Answer

Hi Hanwen!

I hope I am understanding your question.

Basically if (prevLi) and if (nextLi) is logic that, in effect, says only perform the related insertBefore if that (previous or next, respectively) list item exists, and if it doesn't, do nothing.

By the way, this kind of logic is what they call "truthy" and "falsy".

Essentially if the value exists, it logically is "truthy" (meaning will not resolve to false).

If it does not exist (null), then it logically is "falsy" (meaning will not resolve to true).

More info:

https://www.sitepoint.com/javascript-truthy-falsy/ (a pretty thorough explanation).

Does that answer your question?

I hope that helps.

Stay safe and happy coding!

Hi Peter,

You definitely did answer my question. Thank you so much!!!