-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
JavaScript can be written in three primary locations within an HTML document:
<head>
tagYou can place your script inside the <head>
section of your HTML. This is useful when your JavaScript code must load before the page content.
<!DOCTYPE html>
<html>
<head>
<script>
alert("This script is in the head section.");
</script>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
🔎 Note: Scripts in the head execute before the HTML body is rendered, which may cause issues if the script tries to access elements that haven’t been loaded yet.
<body>
tag (at the bottom)Best practice is to place JavaScript at the end of the body tag so that the HTML content is fully loaded before any JavaScript runs.
<!DOCTYPE html>
<html>
<body>
<h1>This is my page</h1>
<p>JavaScript is below:</p>
<script>
alert("This script is at the end of body.");
</script>
</body>
</html>
✔️ Why at the bottom?
Because it allows the page to load faster, and DOM elements are available when the script runs.
You can write JavaScript in a separate .js
file and link it in your HTML using the src
attribute.
<!DOCTYPE html>
<html>
<body>
<h1>My Web Page</h1>
<script src="script.js"></script>
</body>
</html>
alert("Hello from external file!");
🔒 Tip: This is the most scalable and clean method when working on large projects.
You can combine methods, but avoid cluttering the HTML with too much inline JavaScript.
Modern apps usually use external files for better organization, reuse, and performance.
Q1. How do you write a JavaScript script inside the <head>
tag that displays an alert saying "Page loading"?
Q2. How do you place a script inside the <body>
tag that shows a welcome message when the page loads?
Q3. How can you create an external JavaScript file named main.js
that displays an alert saying "External Script" and link it to your HTML file?
Q4. What happens when you try to access a DOM element in a <script>
placed inside the <head>
before the HTML body is rendered? Demonstrate with code.
Q5. How can you resolve the issue of accessing an element before it is rendered by moving the script to the correct location?
Q6. How do you include two JavaScript <script>
tags in your HTML, one in the <head>
and one at the end of the <body>
? Observe and explain the execution order.
Q7. How can you write an external script that changes the background color of a webpage when it loads?
Q8. How do you create a button in HTML and attach an onclick
event using an external JavaScript file that shows an alert when clicked?
Q9. How do you use the defer
attribute in a <script>
tag to ensure that the external script runs after the HTML document has been fully parsed?
Q10. How do you create an external script that logs a message like "Script loaded successfully" in the browser console?
Q1: Which location is recommended for placing JavaScript in an HTML file for better page load performance?
Q2: Which HTML tag is used to include JavaScript code in an HTML document?
Q3: Which attribute of the <script> tag is used to include an external JavaScript file?
Q4: What is the correct file extension for a JavaScript file?
Q5: What will most likely happen if you place a <script> in the <head> that tries to access a DOM element in the <body>?
Q6: Which of the following is the correct way to include an external script in your HTML file?
Q7: What does the defer attribute do when used in a <script> tag?
Q8: Which of the following is an advantage of using external JavaScript files?
Q9: Which section of an HTML document is not recommended for placing JavaScript code?
Q10: What is the correct syntax for writing a single-line comment in JavaScript?