0

Can someone explain what kind of value the following is actually returning.

I understand the code up until the return part. Isn't it just a bunch of comparisons? What kind of value would come back? It would make sense to me if there was an if statement or something. hopefully my question makes sense.

I am not asking for the actual value being returned but more of the concept behind using the return, thanks.

$.fn.is_on_screen = function(){

    var win = $(window);

    var viewport = {
        left : win.scrollLeft()
    };
    viewport.right = viewport.left + win.width();

    var bounds = this.offset();
    bounds.right = bounds.left + this.outerWidth();

    return (!(viewport.right < bounds.left || viewport.left > bounds.right));
};
1
  • It returns boolean value true or false .... That signifies the method signature is_on_screen. Commented Feb 13, 2015 at 4:12

1 Answer 1

1

When a function is returning a comparison it is returning a boolean.

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

5 Comments

So is that like the same as... using an if statement like if(viewport.right < bounds.left || viewport.left > bounds.right){return:true}
Yes, but it would be return:false because it has a ! before the comparison. The function first defines the variables and then makes the comparison using those variables.
so it seems like a shortcut for if(...){return:true;}else{return:false;} correct?
Right. It is very common to use it.
@user3204142 - if() is a condition which check if its true it will execute whatever inside it. where as "return" is used to return the value when function is executed. return can be anything, number, string, Boolean or function. I am not expert in explaining things but I hope you understand my point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.