Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6235525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:40:57+00:00 2026-05-24T10:40:57+00:00

I just started javascript a few days ago and was playing around with different

  • 0

I just started javascript a few days ago and was playing around with different ways to print the return value of a function.

<script>
function produceMessage(){
    var msg= 'This should print';
    return msg;
}

</script>

<span id="mySpan"></span>

Both these lines of code call methods like write or createTextNode to print the message.

document.body.appendChild(document.createTextNode(produceMessage()));
document.write(produceMessage());

However this line does not call a such a method but the return value is still printing.

document.getElementById('mySpan').innerHTML=produceMessage();

My intuition tells me that in order to have the return value appear on the screen, you need to call some sort of method that would allow it to be printed, like in this example:

document.getElementById('mySpan').innerHTML=document.write(produceMessage());

However this is incorrect and results in undefined output.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:40:57+00:00Added an answer on May 24, 2026 at 10:40 am

    Setting the innerHTML of a element to a string results in the HTML of that element being changed to that string. So,

    document.getElementById('mySpan').innerHTML=produceMessage();
    
    • Calls produceMessage()
    • Sets the innerHTML of element with id mySpan to result

    Your function produceMessage returns a string, so the innerHTML of that element is set to This should print.

    Your next statement,

    document.getElementById('mySpan').innerHTML=document.write(produceMessage());
    
    • Calls produceMessage()
    • Calls document.write() with the result, which appends This should print to the document
    • Sets the innerHTML of element with id mySpan to the return value of document.write

    document.write returns undefined, so the innerHTML of that element is set to the string representation of undefined.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey, I've just started learning JavaScript and I'm making a little script that generates
I just started looking into selenium for automated testing for the past few days
I just started using jQuery in the past few days. I love how it
Pretty simple function here but not working. Just started with Javascript so be gentle
I just started using JSON and found this example from http://imdbapi.com/ : <script type=text/javascript>
I just started reading on Douglas Crockford's Javascript The Good parts where he explains
I just started experimenting with Aptana Jaxer server side javascript engine for my next
I have just started writing my own JavaScript Framework (just for the learning experience),
I have just started learning Jquery and am new to writing javascript (I am
When I first started with Javascript, I usually just put whatever I needed into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.