Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript short condition meaning

What does that js code means?

this.totals || (this.totals={});

i suppose..

if(!this.totals) {
  this.totals = {}
}

is this correct?

like image 857
rvpanoz Avatar asked Feb 08 '23 01:02

rvpanoz


1 Answers

You are almost 100% correct. When || is encountered the first part is evaluated and if it's a truthy value it is returned. If it's a falsey value the second part is evaluated which in this case sets this.totals to {} and returns the evaluation, {}.

like image 139
winhowes Avatar answered Feb 10 '23 07:02

winhowes