This introduction will give explanation about creating and using JavaScript in the HTML.
Vanilla JavaScript is the pure, original version of JavaScript, without any additional frameworks or libraries. It's the basic and foundation for many modern web technologies are built.
- Understanding the Basics: Learning Vanilla JavaScript will provides a strong foundation for understanding about How web pages are work? and How it interact with users?.
- Flexibility: Vanilla JavaScript offers maximum control and flexibility for building the custom solutions tailored to meet specific needs.
- Compatibility: It's compatible with all modern web browsers, ensuring that script works across different devices and platforms.
- Text Editor: Choose a text editor or professional code editors..
- HTML File: Create a new HTML file (e.g.,
index.html
) and open it in your text editor. - Basic Structure: Add the basic HTML structure:
<!DOCTYPE html>
<html>
<head>
<title>My First JavaScript Page</title>
</head>
<body>
<h1 id="heading"></h1>
<script src="script.js"></script>
</body>
</html>
- Create JavaScript File: Create a new JavaScript file (e.g.,
script.js
) and link it to your HTML file using the<script>
tag.
Example:
// Variables
let message = "Hello, World!";
// DOM Manipulation
let heading = document.getElementById("heading");
heading.textContent = message;
-
Running JavaScript:
- Then
select the html file
and run by hitting theENTER
button - Or by
open
or byopen with
option menu associated with that selected file. - After that above actions will be reflected on the web browser.
- Then
-
Best Practices:
- Instead of js files, You can directly write JavaScript for the HTML document via Script Tag.
- The best practice is, Write separate file for JavaScript file to make HTML document clean and neat in the code. And also it gives the code re-usability.