Writing our first program in JavaScript

Writing our first program in JavaScript

In this article, we will be writing our first program in JavaScript and learning how to write JavaScript in HTML documents.

So firstly let’s look at the ways in which we can add JavaScript to our HTML document.

There are two ways in which we can add JavaScript to our HTML document:

  1. Adding the JavaScript in the Head Section of the HTML Document: It is a way to write JavaScript in the HTML document but it is not recommended as some times the JavaScript code takes time to execute and because of that the further code gets blocked and the content of the website didn’t get loaded.
  2. Adding the JavaScript at the bottom of the Body section in HTML document: It is the best and recommended method to write JavaScipt code in HTML document. As in this method the content of the website gets loaded first and then the JavaScript code starts executing. So it doesn’t effects or lags the website and website runs smoothly.

Remember that, we write the JavaScript code between the script tags.

So, now that we have basic knowledge of JavaScript. let’s write a basic hello world program in JavaScript.

To write a code that prints the output in the console. We have a console.log function in JavaScript.

So let’s print hello world in the console using the console.log function.

<html>
<head>
    <title>Document</title>
</head>
<body>
    <p>This is a paragraph</p>

    <script> //
        console.log(‘Hello World’); // JavaScript Code
    </script> //
</body>
</html>

Output:

‘Hello World’ is been printed on the console.

That’s all about writing a basic program of JavaScript.

You may like Introduction to JavaScript | Web Development.

Hope this article will guide you to recognize the basics of how to write JavaScript and still if you have any problems or queries regarding this, post them in the comments section and we will be glad to assist you.

Leave a Reply