As you may know, in JavaScript '' + null = "null" and '' + undefined = "undefined" (in most browsers I can test: Firefox, Chrome and IE). I would like to know the origin of this oddity (what the heck was in the head on Brendan Eich?!) and if there is any aim for changing it in a future version of ECMA. It's indeed pretty frustrating having to do 'sthg' + (var || '') for concatenating Strings with variables and using a third party framework like Underscore or other for that is using a hammer for jelly nail pounding.
Edit:
To meet the criteria required by StackOverflow and clarify my question, it is a threefold one:
- What is the history behind the oddity that makes JS converting
nullorundefinedto their string value inStringconcatenation? - Is there any chance for a change in this behavior in future ECMAScript versions?
- What is the prettiest way to concatenate
Stringwith potentialnullorundefinedobject without falling into this problem (getting some"undefined"of"null"in the middle of the String)? By the subjective criteria prettiest, I mean: short, clean and effective. No need to say that'' + (obj ? obj : '')is not really pretty…