JavaScript Hello world program

However, since this book is online, the browser is a good option for our working environment. In order to prevent you from wasting time on them if you want to focus on another environment, we’ll limit the number of commands (like alert) that are only applicable to the browser (like Node.js).

So first, let’s see how we attach a script to a webpage. For server-side environments (like Node.js), you can execute the script with a command like "node my.js".

Table of Contents

The “script” tag

JavaScript programs can be inserted almost anywhere into an HTML document using the 

…After the script.

You can run the example by clicking the “Play” button in the right-top corner of the box above.


This trick isn’t used in modern JavaScript. These comments hide JavaScript code from old browsers that didn’t know how to process the 

Here, /path/to/script.js is an absolute path to the script from the site root. One can also provide a relative path from the current page. For instance, src="script.js", just like src="./script.js", would mean a file "script.js" in the current folder.

We can give a full URL as well. For instance:

To attach several scripts, use multiple tags:

Please note:

As a rule, only the simplest scripts are put into HTML. More complex ones reside in separate files.

The benefit of a separate file is that the browser will download it and store it in its cache.

Other pages that reference the same script will take it from the cache instead of downloading it, so the file is actually downloaded only once.

That reduces traffic and makes pages faster.

If src is set, the script content is ignored.

A single 

We must choose either an external 


  • We can use a .

About browser scripts and how they interact with the webpage, there is still a lot to learn. But let’s not lose sight of the fact that this section of the course is devoted to the JavaScript language, so we shouldn’t get distracted by browser-specific JavaScript implementations. The browser will be used to run JavaScript, which is just one of several options for online reading but is quite practical.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *