1

In this simple example I have a function that returns a text string. When I try to output the result of the function it just outputs the code of the function. Any ideas?

http://jsfiddle.net/VQz73/

1
  • you have to actually execute the function document.write( hello() );
    – Bruno
    Aug 17, 2012 at 8:17

1 Answer 1

4
document.write(hello());

not

document.write(hello);

The former is outputting the result of the function call. The latter is outputting a reference to the function itself. You weren't invoking it.

As a sidenote, document.write is very old school and has almost no use these days. If you're new to JavaScript I would suggest looking into other means of output such as DOM scripting, e.g.

var p = document.createElement('p');
p.textContent = hello();
document.body.appendChild(p);
1
  • Thank you. Sorry I am a bit of a js Rookie.
    – harryg
    Aug 17, 2012 at 8:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.