What does "object || {}" mean in javascript?

I found below line of code in javascript application.

var auth = parent.auth = parent.auth || {}; 

I know that there is an existing parent Object that is extended with an auth object, but I don’t understand what parent.auth || {} does here parent.auth || {} .

+2
source share
3 answers

parent.auth || {} parent.auth || {} means that if parent.auth undefined, null or false in the logical case, then a new empty object will be initialized and assigned.

or you can understand it as:

 var auth; if(parent.auth){ auth=parent.auth; } else { auth={}; } 
+6
source

this means that the value of parent.auth is false (false, 0, null, undefied, etc.) and then assigns the value {} (empty object) to the auth variable

+3
source

|| is or, then the code returns an empty object if parent.auth is undefined.

Like checking for null, then create a new object if null (from java / C #).

0
source

All Articles