Hello! Welcome to our fifth lesson.
In our last session, we successfully set up your complete local development environment. You installed VS Code, learned how to navigate the browser's developer console, and, most importantly, you connected a JavaScript file to an HTML file and saw your first message appear in the console.
Today, we're going to build directly on that foundation. The learning outcome for this lesson is to write and execute your first JavaScript program using console.log(). While we used this command briefly last time, today we will dive deep into what it is, how it works, and the different ways you can use it. This command will be your single most important tool for the first part of this course, acting as your "eyes" to see what your code is doing.
Part 1: What is console.log() and Why Do We Use It?
Think of the browser's console as a dashboard or a control panel for the JavaScript engine. console.log() is the command we use to send a message to that dashboard. It allows us to "log" (or print) any piece of information, from a simple text message to the result of a complex calculation.
As a beginner, you might wonder why we're printing things to a hidden developer console instead of directly onto the web page. This is a fantastic question that gets to the heart of how we learn to program.
To understand this better, let's watch a short video segment. The instructor provides an excellent explanation for why console.log() is the starting point for every new JavaScript developer.
The JavaScript Console Explained for Absolute Beginners
The video 'The JavaScript Console Explained for Absolute Beginners' by Future Fullstack clearly explains the role of console.log() in your learning journey.
Please watch from 03:04 to 05:15. Pay close attention to the distinction the instructor makes between using console.log() as a beginner versus how a professional developer uses it for debugging.
As the video explained, manipulating the content of a web page (the DOM) is a more advanced topic we'll cover later. For now, console.log() gives us an immediate and simple way to get feedback from our code and see the results of our instructions.
Part 2: Writing Your First Programs
Let's start writing some code! Open the js-course folder in VS Code, and open your script.js file. We will write all of our code for this lesson in this file. Remember the workflow from last time: edit the code in script.js, save the file, and refresh the index.html page in your browser to see the output in the console.
Program 1: Printing Text (Strings)
The most classic first program is one that prints a "Hello, World!" message. In programming, a piece of text is called a string. To tell JavaScript that you are writing a string, you must wrap it in either single quotes (') or double quotes (").
In your script.js file, type the following line:
console.log('Hello, World!');
Now, save the file and refresh your index.html in the browser. Open the developer console, and you should see your message. Congratulations, you've just written and executed a valid JavaScript program!
Let's break down the syntax:
console: This is a built-in object that gives us access to the browser's console..: The dot is used to access a property or a tool from an object.log: This is the specific tool (we call it a method) that we want to use. Its job is to print things.(): The parentheses are what actually execute thelogmethod.'Hello, World!': This is the argument we are giving to the method—it's the data we want it to print.;: The semicolon marks the end of a JavaScript statement. While not always required, it's a very good practice to use it.
Program 2: Printing Numbers and Calculations
console.log() can handle more than just text. You can also give it numbers.
Add these lines to your script.js file, below the first one:
console.log(100);
console.log(42.5);
Notice that numbers are not wrapped in quotes. This is how JavaScript distinguishes between the string '100' and the number 100.
Even better, you can perform calculations directly inside the parentheses. The JavaScript engine will solve the math problem first and then log the result.
Add these lines as well:
console.log(5 + 15); // Addition
console.log(20 * 5); // Multiplication
console.log(50 / 10); // Division
Save your file and refresh the browser. You should see the results of these calculations logged in the console.

Program 3: Printing Multiple Things at Once
A very useful feature of console.log() is its ability to accept multiple arguments. Just separate them with a comma, and they will be printed on the same line, separated by a space. This is great for labeling your output.
Add this line to your script:
console.log('My favorite number is:', 7);
After refreshing, you'll see how the string and the number are combined in the output.
Test your understanding!
Now, try to write a short program yourself. In your script.js file, write JavaScript code to achieve the following, with each result on a new line:
- Print the message:
This is my own program. - Print the result of the calculation
(100 + 200) * 3. - Print the message
My age is:followed by your actual age (as a number).
Show answer
Your code should look something like this:
console.log('This is my own program.');
console.log((100 + 200) * 3);
console.log('My age is:', 35); // Replace 35 with your age
When you run this, the console should display:
This is my own program.
900
My age is: 35
Part 3: Executing Your Program in a Different Environment
In our previous lessons, we discussed the difference between the browser and Node.js as JavaScript runtime environments. So far, we've only run our code in the browser.
However, the beauty of console.log() is that it works in virtually every JavaScript environment. Let's run the exact same script.js file using Node.js, right inside VS Code. This will reinforce that your JavaScript code itself is separate from where it runs.
The following short video demonstrates how to run a JavaScript file using Node.js from within the VS Code integrated terminal.
JavaScript Course for Beginners – Your First Step to Web Development
In this clip from 'JavaScript Course for Beginners' by Programming with Mosh, you'll see how to execute a file using Node.js.
Watch from 13:42 to 15:29. Follow these steps: Open the integrated terminal in VS Code (View -> Terminal, or use the shortcut). Type the command node followed by the name of your file: node script.js. Press Enter and see the output.
After following the video, you should see the exact same output from all your console.log() statements, but this time printed directly into the VS Code terminal.

This demonstrates a powerful concept: you can write your core JavaScript logic and test it with console.log() without ever needing a browser. This is fundamental to building applications with Next.js, which relies heavily on the Node.js environment.
Conclusion
Excellent work! You have now truly written and executed your first JavaScript programs. You have mastered console.log(), the most fundamental tool for getting feedback from your code.
Key Takeaways:
console.log()is a function that prints arguments to a console.- We use it in the beginning to see the results of our code since manipulating the web page directly is more complex.
- Text (strings) must be in quotes (
' 'or" "), while numbers are not. - The JavaScript engine evaluates expressions (like
5 + 10) beforeconsole.log()prints the result. - The same JavaScript code can often be run in different environments, like the browser and Node.js.
In our next lesson, we'll look at what happens when things go wrong. You will learn to identify and fix basic syntax errors in JavaScript code. The console, where you've been printing messages today, is also where the JavaScript engine will report errors. Understanding how to read them is the next critical skill on your journey.