Getting started:

Hello, world!

Historically, every programming tutorial must start with a "Hello, world!" example. The purpose of this example is simply to output a message to the user, consisting of the sentence "Hello, world!", without taking any input or doing anything else. It can be traced all the way back to 1974, which was at least 20 years before JavaScript was even invented, but nevertheless, this tutorial will start with a "Hello, World!" example as well.

Now with JavaScript, the place where we output the message may vary, based on the JavaScript engine used to interpret it. For instance, if you run it from a browser, we could use the alert() function to show the message:

alert("Hello, world!");

Try running this example and you'll get a nice popup with the world-famous message to the world. As we talked about previously, this tutorial will start without focusing on the browser, but I do want to mention that if you embed this code in an HTML file, it will have to be inside a SCRIPT tag, like this:

alert("Hello, world!");

Hello, webpage!

With that in mind, I want to show you another example, where we integrate the JavaScript into an actual (albeit simple) webpage, to give you an idea of how it works:

<!DOCTYPE html>
	<title>Hello, world!</title>	

<p>And now for the world famous message:</p>

document.write("Hello, world!");

<p>Thank you, and goodnight!</p>


Try running this example, either directly here in the article, or by saving it as an .html file on your computer and opening it in your browser.

Don't worry too much about all the various HTML tags, which are just a basic structure of a webpage with a bit of text. Instead, focus on the script block in the middle. I have used it to integrate a piece of JavaScript code directly within the HTML code (so basically in the middle of a webpage), and use the document.write() function to output text directly between the rest of the content.

Hello, console!

Be aware that document.write() is a browser-specific function, so if you're running this with Node.js, it likely won't work. Instead, we can use a function that will work across many JavaScript engines: console.log()

console.log("Hello, world!");

If you run this in the browser, you will have to look in the developer console to read the message. How you access this area depends on which browser you use, but you can try pressing F12 - if that doesn't work, you may want to check out this article, which explains how you access it in various browsers.


With the mandatory "Hello, world!" example taken care of, we're now ready to move on and learn more about the JavaScript language.