How to write JavaScript

Sandipan Kr Bag Feb 17, 2024
How to write JavaScript

How to write JavaScript ?
You can write JavaScript code in three places on an HTML page or webpage: internal, inline, and external, just like CSS.

Inline usage: 

You can write JavaScript code within the <script> tags where the </body> tag ends inside the <body> tag. That means within the same HTML file.

Code of Practice:

 <html>
<head>
</head>
<body>
<script>
    document.write("Hi Friens");
</script>
</body>
</html>

Internal usage :

Similar to inline usage, for internal usage, it needs to be written within the <head> tag. Inside the <head> tag, we start with the <script> tag, and then end with the </script> tag. The JavaScript code is written between the opening <script> and closing </script> tags.

 Code of Practice:

<html>
<head>
<script>
    document.write("Hi Friens");
</script>
</head>
<body>

</body>
</html>


Note: Writing this code inside the <head> tag is not the correct approach. Because when you write code inside the <head> tag, if the size of that code becomes large or if the code becomes complex, it will increase the loading time of the webpage, resulting in decreased performance. Since JavaScript code is in the heading section, the browser will attempt to run the JavaScript code before the HTML and CSS code during page loading.

If this heavy or complex code is placed at the bottom of the <body> section, the HTML and CSS will run before the JavaScript code during page loading. As a result, there will be no issues with page loading.

If the script code is lightweight or minimal, you can still place it in the <head> section.

External usage:

 External usage means that the codes will be kept in another file outside of the main HTML file. Then, that file will be linked or included within the HTML file.


Note: When saving the JavaScript external file, it should be named with a .js extension after the file name. .js is the extension for JavaScript files. If the file is not saved with the .js extension, the codes written inside it will not function properly because the browser won't recognize it.


As we save HTML files with the .html extension, similarly, JavaScript external files should be saved with a .js extension.

To link or include the JavaScript external file within an HTML file, it can be done in two places: within the <head> section or at the end of the <body> section. Below is an example of how to do it:

<!-- Within the <head> section -->
<head>
   <script src="name.js"></script>
</head>

<!-- Or at the end of the <body> section -->
<body>
   <!-- HTML content goes here -->
   <script src="name.js"></script>
</body>
 

 

 

Share this article

sndp bag

Sandipan Kr Bag

I'm a dedicated full-stack developer, entrepreneur, and the proud owner of ocec.org.in , hailing from the vibrant country of India. My passion lies in creating informative tutorials and sharing valuable tips that empower fellow artisans in their journey. With a deep-rooted love for technology, I've been an ardent enthusiast of PHP, Laravel, Angular, Vue, Node, JavaScript, jQuery, Codeigniter, and Bootstrap from their earliest days. My philosophy revolves around the values of hard work and unwavering consistency, driving me to continuously explore, create, and share my knowledge with the tech community.

* Hire Me

0 Comments

Load more Comments

Post a Comment

Hint: Please enter between 80 - 300 characters.
ocec.org.in

helllo