AI Features

Getting Started with JavaScript

Learn how to write to the console in JavaScript.

Learning programming often begins with executing a “Hello World!” program. In some languages like Java, it takes a lot to write Hello World to the standard output. In JavaScript, we get away with one single line:

JavaScript
Node.js
console.log( "Hello World");

Let’s try writing it differently

Notice your statement can span multiple lines. I have pressed enter inside the expression to separate content into two lines. You can run your reformatted code in the code editor below:

Node.js
console
.log(
"Hello world!"
);

As you can see, you can format JavaScript code in any way you want. The interpreter will not care about the redundant whitespace characters.

Instead of “Hello World!”, I have written 5 + 2. Run and check what do you see. Experiment a bit more with the log in the code editor below:

Node.js
console.log( 5 + 2 );

Printing more than one value

console.log may print any number of arguments separated by commas. In the console, the values appear next to each other, separated by a space.

Node.js
console.log("Hi",
"Hey",
"Hello :)");

Congratulations! You managed to write Hello World! to the console. Let’s see what we learned:

  1. console.log writes a log message to the console.
  2. “Hello World!” is a string. One way to formulate a string is using double-quotes. Mind you, ‘Hello World!’ is also a valid string notation in JavaScript.
  3. There is a semicolon at the end of the statement. The semicolon itself is optional, but I recommend using it.
  4. You can write more than one value to the console, which will be displayed separated by space.