-2

I have some code examples like this

---------------------- First example -----------------

var rightFunction=function() {
        if (options.currentPage < options.numberOfPages) {
            options.currentPage += 1;
            ajaxConnection();
        }
        return false;
    };

    $('#newsContainer #rightSlider').click(rightFunction);
    $('#rightSliderHigh').click(rightFunction);

---------------------- Second example-------------------

 $('#rightSliderPagination').click(function(){
        if (parseInt(_pagina_last_link.html()) >= options.numberOfPages) {
            return false;
        }
        _first_page_num = parseInt(_pagina_first_link.html());
        paginationGenerator(_first_page_num + options.centerNumber - 1);
        $('#slider-inner').slider("option", "value", _first_page_num + options.centerNumber - 1);
        return false;
    });

------------------------Third example ----------------------

$(".download").click(function(){
        if($(this).attr('goToPage')=='true') {
            var url=$(this).attr('href');
            document.location.href=url;
            return false;
        }

        var _this = $(this);
        var thistext = $("#bg-download-box").html();
        var _whattoshow = $(this).parent().find(".what-to-show").html();

        if (_whattoshow){
            $("#bg-download-box form").html(_whattoshow);
        }

        else{
            $("#bg-download-box").html("<div id='bg-download-box-inner' style='height:132px'></div>");
        }


        if ($(".reccomend-box").length != 0){
        $("#rightColumn").css("z-index",99999);
        }

        $("#bg-download-box").show();
        var whattocopy = $("#bg-download-box");
        $(whattocopy).insertAfter(_this);
        $("ul#songs-list").css("z-index","99999");


        $("ul#songs-list li.b-a-r").removeClass("emph");

        $(this).parent().parent().parent().addClass("emph");
        return false;
    });

Why is return false is used at end of these functions. Please, somebody could explain the purpose of return false.

5
  • Please explain what your code suppose to do to make it easy for people to help you Commented Jul 24, 2014 at 21:01
  • Where is that code from? I don't think there's any reason why those functions need to return any value. Here's the relevant jQuery documentation: api.jquery.com/click Commented Jul 24, 2014 at 21:02
  • @Khalid, I think the wall o' code is irrelevant to the question. OP only cares about return false Commented Jul 24, 2014 at 21:02
  • 2
    google.com/search?q=why%20return%20false%20in%20javascript Commented Jul 24, 2014 at 21:02
  • So as not to trigger the default behavior. Commented Jul 24, 2014 at 21:03

1 Answer 1

1

In Examples 2 & 3, the first return false is preventing the execution of any further code in the function.

In Example 1, it really doesn't do anything. I do this myself though sometimes, just out of habit and clarity sake for future programmers who read my code.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.