Doubt on HTML code in project 3 Controlling Led

In project 3 named controlling LED, I have one doubt, In html code inside head tag, why we use two script tag. why can’t we put that second script tag code inside first script tag code to reduce the code length. Please reply as soon as possible!.

@ramanenvenki We use the first script tag to import an external javascript file, as you can see it has the src attribute.

Look at the second one, we write our own javascript, no src attribute.

This is the reason we use two tags.

@ramanenvenki Also we usually use multiple script tags in order to import more than one javascript files in a single html file, same is the case with css. If you have multiple css files for a single html file then you have to write multiple link tags to import them. Unfortunately we can’t do that in a single line :frowning:.

1 Like

First script tag to import an external javascript file (src attribute)
In second one, we write our own javascript (no src attribute)

here, we use first script tag to import an external javascript file.
in second script tag we use to set key i.e our own javascript.

In HTML, we often use two <script> tags inside the <head> tag for distinct purposes:

  1. External JavaScript : The first <script> tag typically links to an external JavaScript file using the src attribute. This separates HTML and JavaScript code for better organization and allows for code reuse across multiple HTML pages.

  2. Inline JavaScript : The second <script> tag contains inline JavaScript code directly within the HTML file. This allows for specific page-level scripting and is useful when you need unique functionality for that particular page.

Combining both scripts into one <script> tag can be done, but it’s not recommended for maintainability and separation of concerns. Keeping them separate keeps code clean, promotes reusability, and makes it easier to manage and debug.