Hello World!

The first program anyone ever writes.

Pretty much any tutorial or lesson on programming will have you "print" something to the screen as your first program. We tend to use "Hello World!" as that first string, because it's fun.

Output is the only way the program can give feedback to you, the user. So let's start by creating a simple program that outputs "Hello World!".

Javascript does not have an output "window" perse, but it has (at least) two different ways you can print output.

Method 1: The console A console is a text area, a box, or a 'place' where a program can give text output, usually meant to be hidden from the user but sometimes it is the only input/output window (DOS or BASH prompts). Javascript can give output by "logging" to the console window:

HelloWorld.js
// Print to the console
console.log("Hello World!");

Method 2: Writing to the current document Javascript is primarily meant to provide interactivity with a web page (HTML). So, if you are running your Javascript as the back-end to a document, you can write to that document directly:

HelloWorldDoc.js
// Write to the main document (this adds to whatever is already there)
document.write("Hello World!");

// OR you can overwrite the entire document
document.documentElement.innerHTML = "Hello World!";

Last updated