0% found this document useful (0 votes)
141 views

First Hello World Program in JavaScript

This document discusses two ways to add JavaScript to HTML: embedding JavaScript directly in <script> tags and linking to an external JavaScript file. It provides an example of embedding a simple script to write an H1 tag directly in the HTML document. It also provides an example of putting the JavaScript code in an external .js file and linking to it from the HTML using the <script> tag's src attribute. This keeps the code organized when the JavaScript is larger. The output is the same in both cases - the text is displayed on the page.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
141 views

First Hello World Program in JavaScript

This document discusses two ways to add JavaScript to HTML: embedding JavaScript directly in <script> tags and linking to an external JavaScript file. It provides an example of embedding a simple script to write an H1 tag directly in the HTML document. It also provides an example of putting the JavaScript code in an external .js file and linking to it from the HTML using the <script> tag's src attribute. This keeps the code organized when the JavaScript is larger. The output is the same in both cases - the text is displayed on the page.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

First Hello World Program in JavaScript | 2

Ways to add JavaScript to HTML


In this tutorial post we will be running our first JavaScript program which is a basic Hello World program where we ill
simple print Hello World(or any other text) on the browser.

The 2 softwares needed to start of with JavaScript are –

1. Text Editor : I will be using Visual Studio Code Text Editor which is highly recommended by many
specially for Javascript, however you can use any other text editor like Notepad++ or Sublime text etc.
2. Browser : Of course a browser is needed to load the HTML document and run the JavaScript so I will be
using Google Chrome broswer. You can use any browser of your choice as most modern browsers have JavaScript
enabled by default.
There are 2 ways to include JavaScript in your HTML Document –

1. Embedding JavaScript in the same HTML document


2. Using External JavaScript file and linking it in your HTML document.
1. Embedding JS in HTML document –
As you can see in the code below, we are embedding JS code using the <script> tag of HTML. Usually when the JavaScript
code is small it is directly embedded in the HTML document, however, it is not recommended to embed large sized JS code
directly in the HTML document as the code becomes un-organized.

default.html file –

1 <html>
2 <head>
3 <title>My title</title>
4 <script type="text/javascript">
5      document.write("<h1>JS Embedded</h1>");
6 </script>
7 </head>
8 <body>
9 </body>
10</html>
Output – 

2. External JavaScript File –


As you can see below there are 2 file – one the actual HTML document and other a separate external javascript file
(demo.js). This demo.js file is linked in the HTML document using the script tag with the use of the attribute src as shown in
the code below. When the javascript code is large, it is highly recommended to use a separate external file to keep the HTML
doc and the JS code organized.

default.html file –

1<html>
2<head>
3<title>My title</title>
4<script src="demo.js" type="text/javascript"></script>

6</head>
7<body>
8</body>
9</html>
demo.js file –

1document.write("<h1>External file</h1>");
Output – 

So thats it for this basic hello world starter program where we simple printed text on the HTML document(not exactly hello
world but you get the point right

You might also like