tutorialstonight logo


Hello World In Javascript

In this section you will learn to write the first program in javascript with a brief explanation of code, and will also look on how to embed javascript code in HTML pages.

To write javascript code in an HTML document we have to enclose the code in <script> tag. The script element can be used either to write javascript code within the document or it can link external javascript files to link to the document.

Internal Javascript Code

Javascript code can be written within the same document inside <script> tag. The script tag can lie inside the head tag as well as in the body tag.

Here is an example of code written with in the document :

It can be seen here the javascript code document.write("Hello World!"); is written inside the <script> tag within the same HTML document.

External javascript Code

Code for javascript can be written in a separate file which is saved with .js extension. Then this external file can be connected to your documents like HTML.

The external file is connected to the HTML document by script tag with src attribute. Example : <script src="URL"></script> .

Here is an example of code written in the external document :

Again <script> tag can be placed inside head or inside body but it is recommended to place it inside body tag at the end of every other tang inside it.

Understanding Code

Javascript code for this section is very simple: document.write("Hello World!");.

The document in the code represent the HTML document it is connected to, it is itself an object.

write is a method in javascript which directly writes anything in the document.

A semicolon ; is added at the end of every javascript statement. It shows the end of the statement. Code also runs without semicolons.