Next Slide 2 - Why nextSlide have value of 0


#1

I am having a little trouble understanding the fundamentals of why the length of the next sibling following the final element is 0.
var currentSlide = $('.active-slide') ;
var nextSlide = currentSlide.next();
if(nextSlide.length == 0){
nextSlide = $('.slide').first();
}
I would like to undertand this further.

Thank you.


#2

Imagine you are going through your ABC's except in JavaScript, you start counting from 0. So A is 0 B is 1, and so forth. Once you get to Z or 25, you must restart to A or 0 or the first element.
This will all make a lot more sense once you take the JavaScript course.


#3

It's because if you reach the end where there's no next slide you reset it to the first slide.


#4

I guess what I'm confused about is why is it length and not value of the next slide?


#5

The value of the next slide is 0, because it is the 1st slide, but in JavaScript you count beginning with 0.

Edit: I understand what you're saying, the if length == 0 part is checking if you have another slide.


#6

@bitrunner65656 Like @cadecodes says, it checks if you have another slide, and if not wraps it around to the beginning.
The reason why it works is because usually nextSlide would be a JavaScript object containing details about nextSlide. If there is no next slide though, that object is empty, so it's length is zero (0).
:slightly_smiling: