Clarified. The primary reason for the second check is that the caretPos may have returned undefined.