1

Taken from a box2djs sample.

I'm trying to understand the library, but I do not understand the line:

    ballSd.radius = rad || 10;

what does it mean?

Here's the full definition

createBall2 = function(world, x, y, rad, fixed) {
    var ballSd = new b2CircleDef();
    if (!fixed) ballSd.density = 1.0;

    // what does the next line do?
    ballSd.radius = rad || 10;

    ballSd.restitution = 0.2;
    var ballBd = new b2BodyDef();
    ballBd.AddShape(ballSd);
    ballBd.position.Set(x,y);
    return world.CreateBody(ballBd);
};
MedicineMan
  • 15,008
  • 32
  • 101
  • 146

6 Answers6

4
ballSd.radius = rad || 10; 

means: if rad == true (or truthy) return the value of rad, otherwise return 10

KooiInc
  • 119,216
  • 31
  • 141
  • 177
2

A boolean expression in JavaScript does not return false or true, but the first operand (from left to right) that determines the outcome of the expression.

When using logical OR ||, this is the first operand that evaluates to true (similarly the first operand that evaluates to false for &&).

As others already noted, if rad evaluates to false (e.g. if it is 0), then the second operand is returned.

This "trick" is often used to set a default value.

Read more about logical operators.


†: That is only 66.6% correct. The NOT operator ! will always return a boolean value.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • It is not the first operand from left to right that determines the outcome. In the OP's example, the first operand could be a string, but if it's not "falsy", the expression returns a number. It is the operand that causes the comparison to short-circuit that is returned. If no operand short-circuits the comparison, the last operand is returned. The `!` is not really relevant here since it's a unary operator. – Ruan Mendes Mar 03 '11 at 09:02
  • @Juan Mendes: I'd say this is the same. Don't know what you want to tell me with the string example. Of course if `rad` is an empty string it does not determine the outcome. `10` is returned. If it is not an empty string, than it will be returned. Note that I wrote ***e.g.** if it is 0*. And if the last operand is returned then it is the first one that determines the result as all the ones before did not. – Felix Kling Mar 03 '11 at 09:04
  • Now I see what you mean. I was confused because your first sentence is ambiguous, read it again... "the first operand (from left to right) that determines the outcome of the expression." It would be clearer if it said something like "the first operand (from left to right) that short-circuits the comparison determines the outcome of the expression." – Ruan Mendes Mar 03 '11 at 09:11
  • @Juan Mendes: Mmh. I can read as often as I want, I cannot understand it in any other way ;) But you have valid point, this is also because of short-circuit evaluation. – Felix Kling Mar 03 '11 at 09:16
1

All the answers are correct, but they are missing an explanation of the && and || operators in JavaScript. The trick is that they don't return a boolean, they return the value where the comparison short-circuited.

For example

// Returns the first truthy value (2) since after looking at 0 and 2, we already
// know the expression is true and we don't need to evaluate the last component (3)
alert (0 || 2 || 3) 

// Returns the first falsy value (""), the comparison doesn't even
// evaluate "Hello" and "Dog"
alert( "" && "Hello" &&  "Dog" );

// No short circuiting, so the last value ("fun") is returned
alert( "string" && "fun" )
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
0

if rad is false or 0, set ballSd.radius to 10

Quincy
  • 4,393
  • 3
  • 26
  • 40
0

Set the circle radius to either the given argument "rad" if it was given and bigger than zero otherwise to 10, which makes it the default radius.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
0

see this... so if the value of rad variable converted to a boolean is true, then the rad is returned, otherwise it will return 10; any variable can be converted to a boolean value: null, 0, undefined will be converted to false; not undefined will be converted to true; see implicit boolean conversions in javascript

Alex Pacurar
  • 5,801
  • 4
  • 26
  • 33