34

what does the following code do in java script:

var x = x || {};
Gabe
  • 84,912
  • 12
  • 139
  • 238
jakoh
  • 341
  • 1
  • 3
  • 3
  • This question is the older one, the other is a duplicate. – Damjan Pavlica Nov 10 '16 at 16:44
  • Logical expressions in JavaScript are evaluated left to right and they are tested for possible "short-circuit" evaluation using the following rules: false && (anything) is short-circuit evaluated to false. true || (anything) is short-circuit evaluated to true. In the example above, if x is true, x becomes the value assigned. If x is false or undefined, the second operand becomes the assignment which is {}. – tony Aug 06 '17 at 20:46

6 Answers6

45

|| is the logical OR.

The expression

var x = x OR {};

should become more obvious then.

If x has a falsy value (like null, undefined, 0, ""), we assign x an empty object {}, otherwise just keep the current value. The long version of this would look like

var x = x ? x : {};
jAndy
  • 231,737
  • 57
  • 305
  • 359
  • 2
    excellent answer. i am a c programmer and this didnt make any sense. – jakoh Aug 25 '10 at 06:13
  • 2
    If this didn't make any sense for you, then you might also watch out for `!!value` which returns a boolean representing the truthfulness of `value`, very handy at times. – Ivo Wetzel Aug 25 '10 at 06:19
  • Your long version is incorrect: consider the case when x has previously defined and is set to `null`. `var x = x || {};` will redefine x to an object, whereas `var x = (typeof x !== 'undefined') ? x : {};` will not. The paragraph preceding it is correct though. – Tim Down Aug 25 '10 at 15:48
  • @Tim: true, I forgot about the `typeof null === 'object'` enigma. – jAndy Aug 25 '10 at 16:16
8

If x is undefined (or null, or any other false value), it becomes an empty object.

Gabe
  • 84,912
  • 12
  • 139
  • 238
5

One should never write "var x = x || {};" per se.

The only circumstance where this is not identical to "var x = {};" is when x was previously initialized in the same scope. That is immoral. Note:

function() {
  x = {foo:"bar"};
  var x = x || {};
}

Is the same as, merely more confusing than,

function() {
  var x = {foo:"bar"};
  x = x || {};
}

In neither case is there any reference to the value of the symbol "x" in the global scope.

This expression is a confused variant of the legitimate lazy property initialization idiom:

function( foo ) { 
  foo.x = foo.x || {};
  foo.x.y = "something";
}
Dzhaughn
  • 114
  • 1
  • 4
2

Assign x to a new empty object if it is null (undefined, false) or keep it's existing value if not null.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

unless x is defined or assigned a value it will take empty object as default value..

that is,

for example
x = 10
var x = x || {};

output must be

10

if x value not assigned. the output value must be,

undefined
Senthil Kumar Bhaskaran
  • 7,371
  • 9
  • 43
  • 56
-1

if var x is defined then it will be that defined value. Else it will be empty object like [object Object]

For example, in the following code block, x will be 10:

var x = 10;
x = x || {}

However, if:

var x = x || {};

then x will be [object Object]

clinton3141
  • 4,751
  • 3
  • 33
  • 46
Anil Soman
  • 2,443
  • 7
  • 40
  • 64