Getting Started with JavaScript
Learn how to write to the console in JavaScript.
We'll cover the following...
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:
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:
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:
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.
console.log("Hi","Hey","Hello :)");
Congratulations! You managed to write Hello World! to the console. Let’s see what we learned:
- console.log writes a log message to the console.
- “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.
- There is a semicolon at the end of the statement. The semicolon itself is optional, but I recommend using it.
- You can write more than one value to the console, which will be displayed separated by space.