0

I have question because I am not sure and cannot find answer on Stack Overflow about this.

What this exactly mean:

variable = variable || {}

or something that:

this.pointX = options.pointX || 6;

I understand that it assign to variable a variable if it exist or empty Object if variable doesn't exist but why it working that?

Is || not mean 'or' here?

WooCaSh
  • 5,070
  • 3
  • 33
  • 54
  • 1
    This is basically "null coalescing" and you can find more about it here: http://stackoverflow.com/questions/476436/null-coalescing-operator-for-javascript and here: http://stackoverflow.com/questions/6439579/what-does-var-foo-foo-mean-in-javascript/6439629 – Cᴏʀʏ May 24 '13 at 20:23
  • see http://stackoverflow.com/a/6439629/643483 – Aaron Kurtzhals May 24 '13 at 20:23
  • @Cory Like I said. I was looking for this answer but cannot find. Now I found many sources for answer to my question. – WooCaSh May 24 '13 at 20:25
  • 1
    @Cory to be fair, if you don't know the term "null coalescing", it's hard to search for "javascript ||" – jerry May 24 '13 at 20:25
  • @jerry: Very true. I thought the links would help. – Cᴏʀʏ May 24 '13 at 20:25
  • @jerry have right. I cannot write good title because I didn't know what it's called. Be placable. – WooCaSh May 24 '13 at 20:26
  • @Cory Yes, that's a fair point. The first time I read it, it came across differently. Maybe it was prior to the edit or maybe I just misread it. – jerry May 24 '13 at 20:28
  • @Cory We can close this question, because now I found related topics. But while I writing question I still looking in related and cannot find these answers. EDIT: Can I vote to close my own question? – WooCaSh May 24 '13 at 20:35
  • Yes, since you have more than 250 rep, you can [vote to close your own question.](http://stackoverflow.com/privileges/view-close-votes) – jerry May 24 '13 at 20:56

6 Answers6

3

The || is effectively working like a SQL COALESCE statement.

var x = y || z;

means:

if y evaluates to a "truthy" value, assign y to x.

if y evaluates to a "falsy" value, assign z to x.

See http://11heavens.com/falsy-and-truthy-in-javascript for more detail on "truthy/falsy" (or just google it).

pete
  • 21,669
  • 3
  • 34
  • 49
2

The || is an or operator.

It basically means if variable is undefined, it will assign variable to a new object literal.

https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Expressions_and_Operators#Logical_operators

jcreamer898
  • 7,809
  • 4
  • 39
  • 55
  • `variable` is assigned to the second operand any time it has a truthiness value of `false`, not just if it's undefined. For instance, `var s = ""; s = s || "foo";` will result in s having the value `"foo"` even though it had already been defined. – jerry May 24 '13 at 21:13
1

The || operator returns the actual object that determines its "truthiness" value, not just a boolean (true or false). It "short circuits" in that once it can determine the result, it stops.

If variable has a truthiness value of true, it is returned (since when true is ored with anything, the result is true). Otherwise, the second operand is returned (even if it has a truthiness value of false) since it determines the truthiness of the whole expression.

jerry
  • 2,545
  • 1
  • 18
  • 32
1

|| does mean OR here:

var x = 5
var x = x || {} //If v is defined, v = v, else v = {} (new, empty, object).
//x = 5 since x already was defined

var y = y || {}
//y = {} since y was undefined, the second part is run.
Henrik Karlsson
  • 5,127
  • 4
  • 22
  • 40
-1
this.pointX = options.pointX || 6;

Means assign this.pointX the value of options.pointX if available(i.e. not null) otherwise assign the value of 6

Justin
  • 790
  • 2
  • 11
  • 26
-1

The || operator in JavaScript differs from some other languages you'll find it in. When JavaScript evaluates || it seems to return one operand OR the other. It doesn't do a typical truth table evaluation evaluating to true if any operand evaluates to true, and false if not.

Steveo
  • 2,050
  • 1
  • 19
  • 33