Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does "object || {} " means in javascript?

I found below line of code in javascript application.

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

I know there is existing Object parent which is extended with auth Object but i don't understand what is parent.auth || {} doing here.

like image 664
Sandeep vashisth Avatar asked Dec 21 '25 01:12

Sandeep vashisth


2 Answers

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

or you can understand it like:

var auth;
if(parent.auth){       
    auth=parent.auth;   
} else {
    auth={};   
}
like image 138
Zaheer Ahmed Avatar answered Dec 22 '25 13:12

Zaheer Ahmed


it means if the value of parent.auth is falsy(false, 0, null, undefied etc) then assign the value {}(empty object) to the variable auth

like image 37
Arun P Johny Avatar answered Dec 22 '25 14:12

Arun P Johny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!