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 9200369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:45:07+00:00 2026-06-17T22:45:07+00:00

I am new to JavaScript environment.I have read that <script> </script> tag can be

  • 0

I am new to JavaScript environment.I have read that <script> </script> tag can be placed either in the <head> </head> or <body></body> tag of the document. But when I place <script> tag under the <head></head> the output is different. Please review the code:-

<!DOCTYPE html>
<html>
<head>
<script>
function myFunction(a,b)
{
return a*b;
}

document.getElementById("demo").innerHTML=myFunction(4,3);
</script>
</head>
<body>

<p>This example calls a function which perfoms a calculation, and returns the result:</p>

<p id="demo"></p>

</body>
</html>

Output:- This example calls a function which performs a calculation,
and returns the result:

Whereas if <script></script> is placed under <body></body> then I get a different Output. Please find the code and the following output:-

<!DOCTYPE html>
<html>
<body>

<p>This example calls a function which perfoms a calculation, and returns the result:</p>

<p id="demo"></p>

<script>
function myFunction(a,b)
{
return a*b;
}

document.getElementById("demo").innerHTML=myFunction(4,3);
</script>

</body>
</html>

Output:-

This example calls a function which perfoms a calculation, and returns
the result:

12

Any help is highly appreciated.

  • 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-06-17T22:45:08+00:00Added an answer on June 17, 2026 at 10:45 pm

    When the following line of code runs, it searches the DOM for an element with the given ID:

    document.getElementById("demo").innerHTML=myFunction(4,3);
    

    When that code is in the head, that element will not yet exist, and therefore the code will fail. When the code is in the body (after the element in question), it obviously does exist and can be found.

    You could use a DOM ready event handler of some description in your script if you want to keep it in the head. But “best practice” says to place scripts at the bottom of the body.

    In your case, this is the effect you would see depending on the location of your script:

    <html>
        <head>
            <!-- Script here fails -->
        </head>
        <body>
            <!--- Script here fails --->
            <p>This example calls a function...</p>
            <!-- Script here fails -->
            <p id="demo"></p>
            <!-- Script here works! --->
        </body>
    </html>
    

    Note, however, that your function declaration could go anywhere, since it doesn’t do anything before it’s invoked. The invocation is the only part that will have to run after the element exists in the DOM.

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

Sidebar

Related Questions

im new at javascript and i can get this slider to work but not
I'm relatively new javascript, but I am comfortable in other languages. I'm trying to
New to javascript, but I'm sure this is easy. Unfortunately, most of the google
im new to javascript and am intersted in creating a small o3d script: <!DOCTYPE
I have an HTML/JavaScript app that I'm trying to convert to an App using
I have a new mobile application I need to get out the door but
I have Eclipse with PyDev and JavaScript Development Tools (JSDT). I'm using this environment
Let's say I have a javascript method that takes a little to long to
I have some javascript code like this, var worker = new Worker(javascript/worker.js); worker.onmessage =
I have a javascript interface set on my DroidGap class, mc = new MyClass(this,

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.