I expected the following script to display hello, but it display nullhello:
alert(null+'hello');
Why is this so? I can change from null to "", but still would like to understand what is happening.
The value null is stringified to "null", not to the empty string. That's just how it is, there is no "reason" :-)
{}, when coerces to a string, ends up as "[object Object]" (at least in Chrome browser)Javascript concatenates the two strings using the + operator. Consider the following example:
var a = null;
alert(a+'hello');
Even in the above case, the two vars are considered as strings to be concatenated.
In javascript, a default var is initialized as undefined and not null. Consider this
var b;
alert(b+'hello');
.. This will result in undefinedhello