whatsgoingon
whatsgoingon

Reputation: 61

Javascript order of evaluation

Came across this JS snippet, and I honestly have NO idea of what order things are being evaluated in... Any ideas? Parentheses would be helpful...

return point[0] >= -width / 2 - allowance &&
       point[0] <= width / 2 + allowance && 
       point[1] >= -height / 2 - allowance && 
       point[1] <= height / 2 + allowance;

Upvotes: 6

Views: 496

Answers (5)

neeebzz
neeebzz

Reputation: 11538

return (point[0]) >= (-width / 2 - allowance) && (point[0] <= width / 2 + allowance) && (point[1] >= -height / 2 - allowance) && (point[1]) <= (height / 2 + allowance);

Upvotes: 0

Itay Moav -Malimovka
Itay Moav -Malimovka

Reputation: 53597

check this

function bob(n){
  alert(n);
  return n;
}

return bob(1) >= bob(2) / bob(3) - bob(4) &&
       bob(5) <= bob(6) / bob97) + bob(8) && 
       bob(9) >= bob(10) / bob(11) - bob(12) && 
       bob(13) <= bob(14) / bob(15) + bob(16);

Upvotes: 2

mcrumley
mcrumley

Reputation: 5700

https://developer.mozilla.org/en/JavaScript/Reference/Operators/Operator_Precedence

The relavant operators go in this order: unary negation, division, addition/subtraction, relational (>=, <=), logical and.

return (point[0] >= ((-width / 2) - allowance))
    && (point[0] <= ((width / 2) + allowance))
    && (point[1] >= ((-height / 2) - allowance))
    && (point[1] <= ((height / 2) + allowance))

Upvotes: 3

Mikael &#214;stberg
Mikael &#214;stberg

Reputation: 17146

Adding paratheses and some indentation should make it clearer:

return 
  point[0] >= (-width / 2) - allowance 
    && 
  point[0] <= (width / 2) + allowance 
    && 
  point[1] >= (-height / 2) - allowance 
    && 
  point[1] <= (height / 2) + allowance;

Upvotes: 0

Mat
Mat

Reputation: 206659

Equivalent to:

return
    (point[0] >= ((-width  / 2) - allowance))
 && (point[0] <= (( width  / 2) + allowance))
 && (point[1] >= ((-height / 2) - allowance))
 && (point[1] <= (( height / 2) + allowance));

Upvotes: 2

Related Questions