The HTML <script> tag is used to add JavaScript code to a web page. It allows browsers to execute JavaScript programs and make web pages interactive.
The <script> tag is an HTML element used to embed or reference JavaScript code.
Internal JavaScript is written directly inside the HTML document.
JavaScript can be added inside an HTML page.
The browser executes the JavaScript code when the page loads.
The use of the <script> tag has changed slightly from HTML4 to HTML5.
| HTML Version | Requirement |
|---|---|
| HTML4 | Required type="text/javascript". |
| HTML5 | The type attribute is optional. |
An HTML page can contain multiple script tags.
The browser executes them one by one in the order they appear.
Script tags can be placed inside the <head> or <body> section.
| Location | Purpose |
|---|---|
| Head | Functions needed before page loading. |
| Body | Scripts executed after page content loads. |
For better page performance, many developers place JavaScript just before the closing </body> tag.
JavaScript code can be stored in a separate file with the .js extension.
The browser loads and executes the JavaScript file automatically.
Which HTML tag is used to include JavaScript code?