Find out if Next item is equal to last item?

Find out if Next item is equal to last item?

I have some js code in which a click on a nav arrow causes  list to be walked up or down and hilighted. Partial code is below. The problem is that when I get to the end of the list (e.g. there IS no next()), I want to skip this part of the code and actually hide the right arrow. I have no problem with the hiding part - I just want to set a flag when I have traversed the list to the last item.

**Actually, I just need to find if the next item is equal to the last item in the list and set a flag.

Here's the code for highlighting the next list item:
  1. $("#sub_menu .on").removeClass("on").addClass("off").parent("li").next().children("a").addClass("on").click();

I tried something like this:
  1. val1 = $("#sub_menu").parent("li").last();
  2.  val2 = $("#sub_menu").parent("li").next();
  3.   if val1 == val2 {
  4.   var lastItem = true;
  5.    $("#arrow_right").css({visibility:"hidden"});
  6. }