I have a written a simple javascript function that will write the variable value that is outside of the function using document.write and call the function. However, the code doesn't work:
var name = 'guru';
function type () {
document.write = name;
}
I then tried returning document.write. However, this also did not work.
var name = 'guru';
function type () {
return document.write = name;
}
type();
In this code, I just return the variable and use document.write outside of the function. This works.
var name = 'guru';
function type () {
return name;
}
document.write = type();
Why is this happening? Can someone explain this to me?
edit
The code here doesn't work in firebug, because the firebug outside HTML5 document doesn't recognize document.write as a function, see the error in the image. That is if you fire up firebug and run this doesn't work but if it is run inside a HTML DOM this is an error, this is a bug or this is how it works.

document.write (somevariable)document.writefunction? Because none of the three versions seem to do that. They redefinedocument.writeinstead.document.writeas a function, this is correct according to firebug . Wait I ll try it with in the HTML DOM.