0

I have a return statement that was used in this Stackover answer, that I can't quite understand. Here it is:

return maxWidth > $this.width() || maxHeight > $this.height();

What does it mean to return something in way?

I'll edit the title of this question after an answer as soon as I know what it is :)

Community
  • 1
  • 1
shrewdbeans
  • 11,971
  • 23
  • 69
  • 115
  • Are you unclear about the `||` operator, or something else? – DCoder Sep 20 '12 at 15:40
  • It's a logical operator. `||` is "Or" so if the left side is true or the right side is true, return true. If neither are true it returns false. – Shmiddty Sep 20 '12 at 15:42
  • I understand `||`, but it's just the whole combination in that one return statement that perplexes me. – shrewdbeans Sep 20 '12 at 15:43

6 Answers6

7

It's the equivalent of:

if (maxWidth > $this.width() || maxHeight > $this.height()) {
  return true;
} else {
  return false;
}

In other words, if either maxWidth is greater than the width() of $this or maxHeight is greater than the height() of $this, it will return true; otherwise, it will return false.

João Silva
  • 89,303
  • 29
  • 152
  • 158
  • Does this way of returning a value have a special name? – shrewdbeans Sep 20 '12 at 15:45
  • 2
    @Owen: Not really, it's just less verbose, and a common idiom for functions that return a `boolean` value. For example, if you have a function that takes an integer, and returns `true` if the integer is greater than 5, and `false` otherwise, you can just write `return i > 5`, which is less verbose than `if (i > 5) { return true } else { return false }`. – João Silva Sep 20 '12 at 15:47
3

It returns boolean.

return maxWidth > $this.width() || maxHeight > $this.height();

Assume,

maxWidth = 300 
$this.width() = 200
maxHeight = 400
$this.height() = 500

so it returns

(300>200 || 400>500) ==> (T || F) ==> TRUE
Siva Charan
  • 17,940
  • 9
  • 60
  • 95
1

In that particular example the code is checking if the biggest child dimension exceeds the parent dimension, the dimensions being width and height.

Tim Lamballais
  • 1,056
  • 5
  • 10
1

It's known as short-circuit evaluation, and in this case will return a boolean value. If

maxWidth > $this.width() 

is true, it'll return true, without evaluating the second test. Otherwise it'll return the result of evaluating

maxHeight > $this.height(). 
ultranaut
  • 2,132
  • 1
  • 17
  • 22
0

It returns true if one of the dimension of $this, which is a jQuery wrapper object created as $(this), is smaller than some variables.

In the code you link too, that enables the detection of overflowing as maxWidth and minWidth are the dimensions of the biggest child : if one child is bigger than this, then it is overflowing this.

Have a look at the width function.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
0

its a bool value, so if the maximum of width or height is greater than real width, then you get true.

zen
  • 122
  • 10