Mark A. Donohoe
Mark A. Donohoe

Reputation: 30458

in Javascript, why would you write 'b || (b = a);'?

Digging through the glMatrix-0.9.5.min.js source used in my webGL project and I came across several lines of code like this...

vec3.negate = function (a, b)
{
    b || (b = a); // <-- What exactly does this line do?

    b[0] = -a[0];
    b[1] = -a[1];
    b[2] = -a[2];

    return b;
};

Not sure what that code is doing or even if it's a bug considering it's a third-party file, but I also know I'm not completely up to speed about JavaScript as a language. (For instance, I just learned about protocols because of this. Odd/interesting concept.)

So is that valid, and if so, what exactly is it doing?

My guess is it's shorthand for the following, saying 'If 'b' isn't set, set it to a'

if(!b)
{
    b = a;
}

which can also just be written

if(!b) b = a;

which I'd argue is much more clear. But again, I'm guessing as to what that actually means/does. Could be wrong.

Follow-up:

Are these two if-conditions equal?

if(!b){ ... }

if(b == undefined){ ... }

I'm wondering if there's a complication between 'undefined' and a defined value that's 'null'

Upvotes: 1

Views: 109

Answers (4)

Rion Williams
Rion Williams

Reputation: 76597

It's basically setting the value of b to a if b is undefined via the || operator which can be used as a null-coalescing operator in Javascript.

You could think of it in terms of an if-statement as follows :

if(b == undefined){
   b = a;
}

A Matter of Preference

It's ultimately a matter of preference with regards to what makes the most sense, but any of the approaches that you'll find in this discussion are likely valid options :

// Explicitly using undefined in the comparison
if(b == undefined) { b = a }
// Using an if-statement (with a not) 
if(!b){ b = a }
// Using a ternary operator
b = b ? || a

Regarding Your Follow-up

Follow-up: Are these two if-conditions equal?

if(!b){ ... }

if(b == undefined){ ... }

I'm wondering if there's a complication between 'undefined' and a defined value that's 'null'

Yes, there can be differences as seen with an empty string, which would have the following results :

var b = '';
!b                // true
b == undefined    // false

Differentiating null and undefined values can be tricky and since it's a bit of out the scope of this question, you might consider checking out this related discussion on the topic, which commonly recommends the use of if(b == null) { b = a; } as opposed to checks against undefined.

Upvotes: 1

ken4z
ken4z

Reputation: 1390

This is shorthand for if (!b) { b = a }

Lets break it down:

To the left of the || it is asserting on the truthiness of b http://james.padolsey.com/javascript/truthy-falsey/

If b is truthy, then the part to the right of the || will not be evaluated. If b is falsey, then b will get assigned the value/reference of a.

Upvotes: 2

Jordan Davidson
Jordan Davidson

Reputation: 419

a better way to write that would be

b = b || a;

Upvotes: 4

Grissom
Grissom

Reputation: 1082

That means:

b = b ? b : a; //or
b = b || a;

Upvotes: 2

Related Questions