0

I was wondering for a longer time what the below means:

var a = a || { b : 1 }

if 'a' had any properties assigned before... they dissapear. So what is a purpose of the above syntax ?

Oskar Szura
  • 2,469
  • 5
  • 32
  • 42
  • 1
    http://stackoverflow.com/questions/2851404/what-does-options-options-mean-in-javascript – devnull Aug 06 '13 at 09:20
  • if `a` is `false` / `undefined`, then a will equal `{b:1}`. It's really hard to tell what the original `purpose`of the code was. We didnt write it. – Alex Aug 06 '13 at 09:21
  • how this syntax is called? I didn't know how to search it. – Oskar Szura Aug 06 '13 at 09:21
  • Is this really valid? Can you evaluate `a` in it's initialization state like that? Just curious. – Eric Aug 06 '13 at 09:21
  • @OskarSzura https://www.google.com/search?q=javascript+logical+or+assignment&oq=javascript+logical+or+assign&aqs=chrome.1.69i57j0l2j69i62l3.6559j0&sourceid=chrome&ie=UTF-8 – Alex Aug 06 '13 at 09:22
  • Id think it is - I can see it really often. – Oskar Szura Aug 06 '13 at 09:23
  • Yes I know it 'logical OR' but it this some kind of a design pattern ? – Oskar Szura Aug 06 '13 at 09:24
  • 3
    @Eric Its perfectly valid. – Alex Aug 06 '13 at 09:25
  • 1
    @OskarSzura It's called the `default pattern`. See https://www.google.com/search?q=javascript+default+pattern&oq=javascript+defa&aqs=chrome.0.69i59j0j69i57j5j69i62l2.2629j0&sourceid=chrome&ie=UTF-8 – Alex Aug 06 '13 at 09:27
  • @OskarSzura But I wouldn't get so hung up on if its a pattern or not. Its just something thats really nice to do, hence why you see it a lot. – Alex Aug 06 '13 at 09:28

4 Answers4

6

I love this kind of syntax. In my opinion, it is very elegant.

The explanation is fairly simple. What you have here is a conditional expression using the || (or) operator. This will assign a value to the variable according to the result of the conditional.

In this case, the condition is a || { b : 1 }, so if the variable a has already been defined (i.e. not equal to a false value), then the variable a will be left unchanged. However if a has not been defined yet, then it will be assigned the value of the object literal { b : 1 }.

This method of syntax is usually used to define default values.

For example:

function say_hello( name ){
  var the_name = name || 'Dude';
  alert( "Hello, " + the_name );
}

say_hello( "Lix" ); // OUTPUTS: Hello, Lix
say_hello(); // OUTPUTS: Hello, Dude

If the argument name has not been passed to the function, the default name Dude will be used.

Kevin
  • 7,162
  • 11
  • 46
  • 70
Lix
  • 47,311
  • 12
  • 103
  • 131
  • 1
    yup, I was taught once how to use this syntax for defaulting arrays say `arr[0] || arr[7]` on an array of size 3 will give the first element on array i.e. `arr[0]` –  Aug 06 '13 at 09:56
  • @rps - yep. That is indeed a valid usage of this method. However I would argue that the default value should be on the *index* before you access the array and not on the array access itself. – Lix Aug 06 '13 at 10:48
  • but how ? `arr[7]` is falsy but `7` isn't. –  Aug 06 '13 at 11:18
  • I was thinking more on the lines of testing if the index is out of range first. – Lix Aug 06 '13 at 11:20
  • ah yes ofcourse, just for the fun of using _elegant_ codes one shouldn't skip the basics. –  Aug 06 '13 at 11:28
3

if a is falsy ie (false, 0, undefined, null, "", NaN) assign the default value { b : 1 } to it

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
1

The code assigns the object { b: 1 } to a if a is undefined.

a || { b: 1 } means a or { b: 1 } and the || operator returns the first operand which is true. So if a defined it will returned a otherwise it will return { b: 1 } (since that is true).

jbr
  • 6,198
  • 3
  • 30
  • 42
0

If a has some value already assigned then it will take that values else it will assign object {b:1} to a.

Lix
  • 47,311
  • 12
  • 103
  • 131
Deepak Biswal
  • 4,280
  • 2
  • 20
  • 37