In HTML, the <script> tag is used to include JavaScript code within an HTML document. The <script> tag can be placed in the <head> or <body> section of the HTML document.
Here's an example of how to use the <script> tag:
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Script Tag Example</title>
<script>
// JavaScript code goes here
function greet() {
alert("Hello, World!");
}
</script>
</head>
<body>
<h1>Welcome to my webpage</h1>
<button onclick="greet()">Click me</button>
</body>
</html>
In the example above, the JavaScript code is placed within the <script> tag in the <head> section of the HTML document. The code defines a function called greet() that displays an alert box with the message "Hello, World!".
The <script> tag can also include external JavaScript files by specifying the src attribute:
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Script Tag Example</title>
<script src="script.js"></script>
</head>
<body>
<h1>Welcome to my webpage</h1>
<button onclick="greet()">Click me</button>
</body>
</html>
In this case, the JavaScript code is stored in an external file called "script.js". The src attribute specifies the URL or relative path to the external JavaScript file.
Including JavaScript code using the <script> tag allows you to enhance the functionality of your web page by adding interactivity, manipulating the DOM, making AJAX requests, and much more.