Skeleton of JavaScript

Let us formally start learning JavaScript in completely different, less boring way. Every language that we speak and write has some structure. If we talk about English, there is subject and predicate. Without predicate, you cannot understand what the person is talking about. If the subject is missing in the sentence, it would be impossible to tell whom it is being talked about. Likewise, JavaScript has a certain format, which browsers and other development environment interpret the way we want them to interpret.

JavaScript Mandatory Tag <script>:

JavaScript uses JavaScript statements that are placed within the HTML tags in a web page. You can place the <script> tags, containing your JavaScript, anywhere in your web page, but it is recommended that you should put it within the <head> tags. The <script> tag alerts the browser program to start interpreting all the text between these tags as a script. A <script> tags of JavaScript in which you will put your JavaScript code looks as follows:

<script>
    // Your JavaScript Code Here
</script>

Within the first tag, which is <script>, you have to mention two attributes. The question arises what is an attribute? In simple terms, an attribute is a feature regarded as a characteristic or inherent part of someone or something. In JavaScript case there are two mandatory attributes, which are as follows:

  1. Language tag
  2. Type tag

Both the attributes will look like this:

<script language="javascript" type="text/javascript">

The value of language attribute must be javascript, and the type-attribute must have the value text/javascript.

Now that we know the basic skeleton of the JavaScript tags, let’s take a look at the example!

First Full-fledged Example:

Following are the steps you should follow to understand the anatomy of JavaScript. Step 1: Create an HTML document. Although JavaScript must have its own file with the extension “.js”, but for this article I am going to place the code in the HTML document. Step 2: After creation of the HTML document, write the structure of HTML. As this article is focusing on JavaScript, I will not focus on the HTML part much. The HTML code would look like this:

<!DOCTYPE html>
<head>
    <!-- Head of HTML -->
</head>
<body>
    <!-- Body of HTML -->
</body>
</html>

Step 3: Now that we have written the usual structure of HTML, we can place JavaScript code in it. As I have mentioned before that the head of HTML is the recommended place for JavaScript code. The JavaScript code would look like this:

<!DOCTYPE html>
    <head>
        <!-- Head of HTML -->
        <title>
        My First JS Program
        </title>
        <script language="javascript" type="text/javascript">
            document.write("Hello from the other side!")
        </script>
    </head>
    <body>
        <!-- Body of HTML -->
    </body>
</html>

Step 4: Save the above code in .html extension file, and then run it in any of your favorite browser. I am using Chrome in this example. The output would look something like this:

enter image description here

As you would see, I have set the title as well, but that is in HTML form. The thing on which you should focus is the sentence “Hello from the other side!” In the code above document.write( ) function of JavaScript refers to the page that is opened in your browser.

Summary:

In this article, you have learned how to use JavaScript <script> and what its mandatory components are. At the end, there was a complete example given to show you the skeleton of JavaScript. That’s it for this article!

AUTHOR

READ NEXT

Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?