JavaScript – Internal JavaScript Code


Including Javascript code is the first step for making an HTML file interactive.

Only when the Javascript code is included / inserted, we can use the HTML DOM dynamic operations and other functionalities by accessing the JS libraries.

How to include JavaScript code in Html ?

Javascript code is included in the html file inside the <script></script> tag.

This <script></script> tag can be written inside both the header and body section of the HTML file.

Script tag written inside the header tag :

Example 1 :

<html>
	<head>
		<title>Our title</title>
		<script>
			Javascript code ...
		</script>
	</head>
	<body>
		HTML body content ...
	</body>
</html>

Script tag written inside body tag :

Example 2 :

<html>
        <head>
                <title>Our title</title>
        </head>
        <body>
                HTML body content ...
		<script>
			Javascript code ...
		</script>
        </body>
</html>

What are types of including JavaScript code ?

We can include JavaScript code in two ways.

1. Internal JavaScript code.

2. External JavaScript file.

What is internal JavaScript ?

Internal JavaScript code is code that’s placed anywhere between HTML tags within the web page.


The Script tag is placed inside the <head></head> tag


<html>
	<head>
		<title>Our Webpage title</title>
		<script>
			alert("HI Good morning");
		</script>
	</head>
	<body>
				
	</body>
</html>

The Script tag is placed inside the <body></body> tag

<html>
        <head>
                <title>Our Webpage title</title>
        </head>
        <body>
		 <script>
                        alert("HI Good morning");
                </script>
        </body>
</html>

Output :

When to use internal JavaScript code ?

We can use JavaScript code internally within our HTML document only limited lines of code required for specific operations in the webpage.

When the usage and operation of the code is very less, we can use internal Javascript code.

What are the advantages of Internal JavaScript?

Internal JavaScript loads much faster than external code.

Internal JavaScript code is loaded when the document is loaded it produces faster results.

Internal JavaScript code is easy to modify and debug the errors.

Javascript interview questions and answers !!!

Click to Learn More about – Javascript online learning