1

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'

LJᛃ
  • 7,655
  • 2
  • 24
  • 35
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286

4 Answers4

4

a better way to write that would be

b = b || a;
Jordan Davidson
  • 419
  • 4
  • 14
2

That means:

b = b ? b : a; //or
b = b || a;
Grissom
  • 1,080
  • 8
  • 21
2

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.

ken4z
  • 1,340
  • 1
  • 11
  • 18
1

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.

Community
  • 1
  • 1
Rion Williams
  • 74,820
  • 37
  • 200
  • 327
  • Not in regards to my code example, but in regards to your answer, if `b` was defined, but was set to `null`, would the first if fail but the second if pass, or if something is null, it's also considered to be undefined? (I'm thinking in the regards of a `parent` field that may be defined but who's value is set to `null` for root-level objects.) – Mark A. Donohoe May 11 '16 at 17:46
  • Yes, if `b` was set to `null`, `b == undefined` would still evaluate as `true`. You can [see an example of this here](http://jsbin.com/sinalo/edit?js,output). – Rion Williams May 11 '16 at 17:48
  • What if `b` was set to '' (zero-length string)? It's definitely defined so `b == undefined` is false, but wouldn't `!b` be true? – Mark A. Donohoe May 11 '16 at 17:50
  • If `b` was an empty string, `!b`would evaluate to `true` and `b == undefined` would be `false`. [Another example here](http://jsbin.com/hiviri/1/edit?js,console) – Rion Williams May 11 '16 at 17:54