How to Link JavaScript File in HTML ?
JavaScript can be added to HTML file in two ways:
- Internal JS: We can add JavaScript directly to our HTML file by writing the code inside the <script> tag. The <script> tag can either be placed inside the <head> or the <body> tag according to the requirement.
- External JS: We can write JavaScript code in another files having an extension.js and then link this file inside the <head> tag of the HTML file in which we want to add this code.
Syntax:
<script>
// JavaScript Code
</script>
Example:
HTML
<!DOCTYPE html> < html lang = "en" > < head > < title > Basic Example to Describe JavaScript </ title > </ head > < body > <!-- JavaScript code can be embedded inside head section or body section --> < script > console.log("Welcome to w3wiki"); </ script > </ body > </ html > |
Output: The output will display on the console.
Welcome to w3wiki