I'm studying JavaScript and I'm confused about the following:
document.getElementById("demo").innerHTML = typeof (document.getElementById("demo").innerHTML);
It assigns "string" to an HTML element. OK. So then I suppose when I will try to assign some value of different type (not string) it will be converted to string first:
document.getElementById("demo").innerHTML = null; //null will be converted to string first
like this:
document.getElementById("demo").innerHTML = String(null);
HTML element gets en empty value in the first example. But the second example returns "string". Why? This tutorial http://www.w3schools.com/js/js_type_conversion.asp says that String(null) returns "null", but not empty string. As I see it's true only for the second example.
How to understand this confusing conversion behavior of JavaScript? Why doesn't the first example return "string"?
Edited:
Now partially I understood it.
JS uses toString() but not String(). And toString() returns empty string for null.
So now my question is: Why toString() and String() produce different values?
Edited 2:
Examples:
document.getElementById("demo").innerHTML = String(null); //returns an empty string
document.getElementById("demo").innerHTML = null.toString(); //returns "null" string
ToString()would also yield"null".toString()are you talking about then?null.toString()... so this is likely to be an implementation detail and shouldn't be relied upon.