JavaScript in body or head: Scripts can be placed inside the body or the head section of an HTML page or inside both head and body.
JavaScript in head: A JavaScript function is placed inside the head section of an HTML page and the function is invoked when a button is clicked.
Examples:
<!DOCTYPE html> <html> <head> <script> function gfg() { document.getElementById("demo").innerHTML = "Geeks For Geeks"; } </script> </head> <body> <h2>JavaScript in Head</h2> <p id="demo" style="color:green;">geeksforgeeks.</p> <button type="button" onclick="gfg()">click it</button> </body> </html> |
Output:
Before clicking the button

After clicking the button

JavaScript in body: A JavaScript function is placed inside the body section of an HTML page and the function is invoked when a button is clicked.
Example:
<!DOCTYPE html> <html> <center> <body> <h2>JavaScript in Body</h2> <p id="demo">geeksforgeeks.</p> <button type="button" onclick="gfg()">Try it</button> <script> function gfg() { document.getElementById("demo").innerHTML = "Geeks For Geeks"; } </script> </body> </center> </html> |
Output:
Before clicking the button

After click on button

External JavaScript: JavaScript can also be used as external files. JavaScript files have file extension .js . To use an external script put the name of the script file in the src attribute of a script tag. External scripts cannot contain script tags.
Example:
<!DOCTYPE html> <html> <center> <body> <h2>External JavaScript</h2> <p id="demo">Geeks For Geeks.</p> <button type="button" onclick="myFunction()">Try it</button> <script src="myScript.js"></script> </body> <center> </html> |
Output before clicking:

Output after clicking:

Advantages of External JavaScript:
- Cached JavaScript files can speed up page loading
- It makes JavaScript and HTML easier to read and maintain
- It separates the HTML and JavaScript code
Recommended Posts:
- How to get the entire HTML document as a string in JavaScript ?
- HTML Course | Structure of an HTML Document
- Difference between PUT and DELETE request in Vanilla JavaScript
- Display the number of links present in a document using JavaScript
- How to write shorthand for document.getElementById() method in JavaScript ?
- Why split the <script> tag when writing it within document.write() method in JavaScript ?
- JavaScript | window.location and document.location Objects
- How to put a responsive clear button inside HTML input text field ?
- HTML DOM document plugins Property
- How to convert an HTML element or document into image ?
- How to specify an URL to a document that explains the reason why the text was deleted in HTML ?
- How to delete text from document using HTML ?
- HTML DOM document contentType Property
- HTML DOM document visibilityState Property
- HTML DOM document scrollingElement Property
- HTML DOM Document hidden Property
- HTML DOM document location Property
- HTML DOM window document property
- How to semantically delete text from an HTML document?
- How to embed audio element in a HTML document ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


