Getting Started with JavaScript
Explore the fundamentals of JavaScript by learning how to write and run simple programs, including printing messages to the console. Understand syntax rules, string notations, and formatting code flexibly while practicing with console output.
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:
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:
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:
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.
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.