I am starting to learn JavaScript and have been using w3schools as my resource. As a new kid on the block I would like to ask a humble and simple question. In layman's terms - What is the difference between:
this code
function myFunction() {
return "Hello World"
}
document.getElementById('demo').innerHTML = myFunction();
and that code
function myFunction() {
document.getElementById("demo").innerHTML = "Hello World!";
}
myFunction();
Reason I am asking is:
I am doing some exercises and I got the end result correctly by doing this code but then when I clicked "show answer" the code shown is that code which is different from mine.
In addition to my question - which one is practical and which one is best for what situation?
reference exercise link.
functionat all? It's pretty useless unless it is executed multiple time. If you do call it multiple times, you'll quickly see how they are different.