Can anyone tell me why in the following code a do/while lop is used rather than a simple if statement:
function prev(elem){
do {
elem = elem.previousSibling;
} while(elem && elem.nodeType != 1);
return elem;
}
Why not:
function prev(elem){
if(elem && elem.nodeType != 1) {
elem = elem.previousSibling;
return elem;
}
Is there an advantage to using do/while? Thanks!
do-whilewill run once and continue running while the statement is true, while theif-statementwill only run once.In this instance it may be equivalent (depending on how the code and data is set up), but typically that is how
do-whilesare used.