Javascript TUTORIAL


Your First Hello World Javascript



No Special compiler, interpreter or server side installation is required to start running javascript. It means that you can start writing javascript on a text editor and open it in a browser. Let up jump start and write the following code in a text file.

<html>
<body>
<script type="text/javascript">
document.write("Hello World!");
</script>
</body>
</html>



The text in the blue is the part of the normal html code and the text in the red is part of the javascript. Save the file as, say, hello.htm. If you open the file in a browser it will produce the following.

Hello World!


Try the Example online


You may like to make changes into the example and see how the things work. Since javascript is supported by all the browsers, you may just write them in a text file and open in a browser to see how it looks like. You may also like to make changes and see the effect online at the link below. The page will open in a new Window.

Javascript Hello World Example

Explanation


The <script> tag is used to insert a JavaScript into an HTML page. Inside the <script> tag we use the "type=" attribute to define the scripting language. So, the <script type="text/javascript"> and </script> tells where the JavaScript starts and ends.

The word document.write is a standard JavaScript command for writing output to a page.

By entering the document.write command between the tags, the browser will recognize it as a JavaScript command and execute the code line. In this case the browser will write Hello World! to the page:

Browsers that do not support JavaScript


If a browser do not support Javascript, it will display the whole content of the javascript that is written between <script> and</script>. To prevent this an HTML comment tag <!-- just before the first JavaScript statement, and a --> (end of comment) after the last JavaScript statement is added as shown below.

<html>
<body>
<script type="text/javascript">
<!--
document.write("Hello World!");
//-->
</script>
</body>
</html>



The two forward slashes at the end of comment line (//) is the JavaScript comment symbol. This prevents JavaScript from executing the --> tag.