String literal first or second in concatenation?

  • A+
Category:Languages

In JavaScript, is there any circumstance where there is a semantic difference between these two options?

foo.bar + '' 

...and...

'' + foo.bar 

I would've expected the latter to more reliably coerce the result to a string, but I can't find any discussion of this (after much Googling) nor any example where it seems to matter.

 


Both are the same.

There is only a difference if there are other + (on the left or the right). In other words:

1 + 1 + ''                          // results in '2' 

Is not the same as:

'' + 1 + 1                          // results in '11' 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: