23

Sometimes I see in javascript code something like this:

var myObj = myObj || {};

So, what actually happen here? I suppose || operator returns true or false, but it's not correct.

Kai
  • 2,023
  • 7
  • 28
  • 49

7 Answers7

40

The || operator returns the left operand if it evaluates as true, otherwise it evaluates and returns the right operand. In other words, a || b is equivalent to a ? a : b except that a is only evaluated once.

cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • 2
    +1 also called *null coalescing operator* in JavaScript http://stackoverflow.com/questions/476436/null-coalescing-operator-for-javascript/476445#476445 – naveen Aug 19 '11 at 08:05
  • 2
    No, it can just be used as a *replacement* for C#'s null coalescing operator (`??`). :) I've never heard `||` called the "null coalescing operator;" it's always been the "logical or" operator. – cdhowie Aug 19 '11 at 08:06
  • agreed. from OP's previous questions he is from ASP.NET background and thats why this clarification. – naveen Aug 19 '11 at 08:10
6

To understand the || operator, let's first look at a fairly basic example. The logical OR operator may be used to provide a default value for a defined variable as follows:

 var bar = false,  
 foobar = 5,  
 foo = bar || foobar; // foo = 5  

In this case, foo will only be assigned the value of foobar if bar is considered falsy. A falsy value could be considered being equal to 0, false, undefined, null, NaN or empty (e.g "").

Joe.wang
  • 11,537
  • 25
  • 103
  • 180
3

This initializes myObj unless it is already defined.

Jan Aagaard
  • 10,940
  • 8
  • 45
  • 80
3

You can use this construct to get the object that is not null, undefined, etc. This is used in cases where you use myObj later on in the code which requires it to be an object. If, for some reason, myObj is undefined prior to this line, re-assigning it leaves it undefined or null, in which case it would be assigned {}.

You can think of this as:

// If the object is already defined
if (myObj)
    var myObj = myObj;
// It was undefined or null, so assign an empty object to it.
else
    var myObj = {};
zatatatata
  • 4,761
  • 1
  • 20
  • 41
2

The OR op (||) will return the first non-empty/false parameter.

In the case specified, if myObj is false or null, it will be set to an empty object (the {} brackets are used to create objects)

Doug Kress
  • 3,537
  • 1
  • 13
  • 19
1

|| is a short circuit operator. If the first operand evaluates to true the second is not evaluated.

Thus JS a || b is something like c# a ?? b

Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95
0

if myObj is undefined or null then it evaluates the expression on the right side of || which creates a new empty object

so myObj is either myObj if it is not null or an empty object if myObj is null

i hope you understand what i mean

Fender
  • 3,055
  • 1
  • 17
  • 25
  • 1
    Also if the value of `myObj` was false or 0 or NaN or the empty string it will also cause the variable to then be assigned a new object. It's not **just** undefined or null. There are 6 falsy things in JavaScript. – Ray Toal Aug 19 '11 at 08:06
  • @Ray Toal: thanks for the addition – Fender Aug 19 '11 at 09:06