5

I'm following the Codecademy "Make an interactive website" course. I don't have an error, but I had a question about something I don't understand: the if-statement used in a slide thing thing thing.

if (nextSlide.length === 0) {
            nextSlide = $('.slide').first();
            nextDot = $('.dot').first();
        };

This if-statement is for that after the last slide it will go to the first slide. Why is it nextSlide.length === 0?

jQuery:

var main = function() {
    $('.dropdown-toggle').click(function() {
        $('.dropdown-menu').toggle();
    });
    $('.arrow-next').click(function() {
        var currentSlide = $('.active-slide');
        var nextSlide = currentSlide.next();

        var currentDot = $('.active-dot');
        var nextDot = currentDot.next();

        if (nextSlide.length === 0) {
            nextSlide = $('.slide').first();
            nextDot = $('.dot').first();
        };

        currentSlide.fadeOut(600).removeClass('active-slide');
        nextSlide.fadeIn(600).addClass('active-slide');

        currentDot.removeClass('active-dot');
        nextDot.addClass('active-dot');
    });
};

$(document).ready(main);
rene
  • 41,474
  • 78
  • 114
  • 152
Coder
  • 302
  • 4
  • 15

3 Answers3

6

The if statement is there to do a check as to if there is a slide after the current slide or if it should revert to the first slide.

nextSlide.length will return a number to represent if the next slide exists, else it will return 0.

.length API Docs: Here

j08691
  • 204,283
  • 31
  • 260
  • 272
Stewartside
  • 20,378
  • 12
  • 60
  • 81
3

If your question is why === instead of ==

This ensures that the variables being compared are of the same type

More reading here: SO Question === vs ===

Community
  • 1
  • 1
StudioTime
  • 22,603
  • 38
  • 120
  • 207
  • @Darren-Sweetney Oh sorry, I wasn't clear enough... :D I meant why it's 0, I made 0 bold but the system blocked it... But actually I also didn't understand that thing of yours :) So thank you anyways I'll watch that == vs ===. – Coder Apr 29 '15 at 13:53
1

That checks if there is a next slide. If it is 0 it means there is no next slide. In JavaScript, the length of emptiness is 0.

Mihey Egoroff
  • 1,542
  • 14
  • 23