Hello World - Your First Program
Hello, JavaScript world!
In this module, we’re going to create our very first JavaScript program. Even if you’ve never even seen code before, don't worry, we'll cover it all—from understanding what each line does to experimenting by changing the code. By the end of this lesson, you'll know how to print text to the console and understand the basics of JavaScript syntax.
Here is a piece of code that we'll start with.
1// We have some code here
2console.log("Hello, World!");What Does the Code Do?
Our program uses a single JavaScript function, console.log(), to display a message. When you run your code, the message will appear in the console. This is an essential tool in JavaScript, used to output information and help debug your programs.
Breaking Down the Code
Let’s look at the code line by line.
Line 1: Comment
In JavaScript, any text following **// **on the same line is a comment. Comments are used to explain the code and are ignored by the computer when the program runs.
☝️** Note**: Good programmers don't normally use comments to describe their code but write readable code that describes itself.
Line 2: console.log("Hello, World!");
- console.log(): This is a function call to the **console.log **function that prints any message inside its parentheses to the console. Functions take parameters inside the parentheses.
- "Hello, World!": This is a string—a sequence of characters enclosed in quotes. In JavaScript, strings are used to represent text.
- **The semicolon ; **ends a statement. We could have multiple prints on a single line like console.log(1); console.log(2); and the semicolon tells the JavaScript interpreter where one statement begins and the other ends. It's kind of like the dot in english!
- Execution: When this line runs, it tells the browser or Node.js environment to display the text "Hello, World!" in the console.
Let's practice!
Learn to hack — start here
Hundreds of interactive courses, virtual labs and CTF challenges in your browser. Start a free trial — no card required.