Introduction
After installing Node.js and Visual Studio Code (VS Code) and setting up your Node.js environment, you are ready to write and execute your first JavaScript program.
Running your first program is an important milestone because it verifies that your development environment has been configured correctly. It also introduces you to the basic workflow of creating a JavaScript file, writing code, saving it, and executing it using Node.js.
In this lesson, you will learn how to create, run, and understand your first JavaScript program.
Prerequisites
Before running your first JavaScript program, ensure that you have:
-
Installed Node.js
-
Installed Visual Studio Code (VS Code)
-
Created a project folder
-
Opened the project in VS Code
Step 1: Create a Project Folder
Create a folder for your JavaScript project.
Example:
JavaScript_Project
Open this folder in Visual Studio Code.
Step 2: Create a JavaScript File
Inside your project folder, create a new file named:
index.js
The .js extension indicates that the file contains JavaScript code.
Step 3: Write Your First JavaScript Program
Open the index.js file and type the following code:
console.log("Hello, World!");
Save the file using:
Ctrl + S
Understanding the Code
console.log("Hello, World!");
Let’s understand each part:
console
console is a built-in JavaScript object used for displaying output.
log()
log() is a method of the console object that prints messages to the console.
"Hello, World!"
This is a string that will be displayed as output.
Step 4: Open the Integrated Terminal
In Visual Studio Code, open the terminal.
Menu:
Terminal → New Terminal
Or press:
Ctrl + `
The terminal opens at the bottom of the editor.
Step 5: Run the JavaScript Program
Ensure that you are inside the project folder.
Run the following command:
node index.js
Output
Hello, World!
Congratulations! You have successfully executed your first JavaScript program.
How the Program Executes
The execution process is as follows:
Write JavaScript Code
│
▼
Save File
│
▼
Run:
node index.js
│
▼
Node.js Executes the Code
│
▼
Output Appears in Terminal
Printing Multiple Messages
You can print multiple lines using multiple console.log() statements.
console.log("Welcome");
console.log("to");
console.log("JavaScript");
Output
Welcome
to
JavaScript
Printing Numbers
JavaScript can also print numbers.
console.log(100);
console.log(3.14);
Output
100
3.14
Printing Boolean Values
console.log(true);
console.log(false);
Output
true
false
Printing Variables
let language = "JavaScript";
console.log(language);
Output
JavaScript
Printing Multiple Values
You can print multiple values in one statement.
let name = "Alice";
let age = 25;
console.log(name, age);
Output
Alice 25
Performing Calculations
JavaScript can evaluate expressions before printing them.
console.log(10 + 20);
console.log(8 * 5);
console.log(50 / 2);
Output
30
40
25
Running Another JavaScript File
Suppose you create another file named:
app.js
Run it using:
node app.js
The command format is always:
node filename.js
Common Mistakes Beginners Make
Forgetting to Save the File
Always save the file before executing it.
Shortcut:
Ctrl + S
Incorrect File Extension
Incorrect:
index.txt
Correct:
index.js
Running from the Wrong Folder
If the terminal is not inside the project folder, Node.js cannot locate the file.
Navigate to the correct folder before running the command.
Typing the Wrong Command
Incorrect:
nodejs index.js
Correct:
node index.js
Common Errors
File Not Found
Example:
Error: Cannot find module 'index.js'
Solution
-
Verify the file name.
-
Ensure you are in the correct directory.
-
Confirm that the file exists.
Node Command Not Recognized
Example:
'node' is not recognized as an internal or external command.
Solution
-
Verify that Node.js is installed.
-
Restart the terminal.
-
Ensure Node.js is added to the system PATH.
Running JavaScript Using the Browser
JavaScript can also run directly inside a web browser.
Example:
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Example</title>
</head>
<body>
<script>
console.log("Hello from Browser!");
</script>
</body>
</html>
Open the HTML file in a browser and check the Developer Tools → Console to view the output.
Running JavaScript Using Node.js vs Browser
| Feature | Node.js | Browser |
|---|---|---|
| Runs JavaScript | Yes | Yes |
| Requires HTML | No | Yes (for webpage execution) |
| Uses Terminal | Yes | No |
| Uses Browser Console | No | Yes |
| File System Access | Yes | No |
Why Automation Engineers Use Node.js
Automation frameworks such as:
-
Playwright
-
Selenium WebDriver (JavaScript)
-
WebdriverIO
-
Cypress
execute JavaScript using Node.js.
Every automation script is typically run using commands similar to:
node filename.js
Understanding how to run JavaScript programs is the first step toward writing automation scripts.
Real-World Example
Suppose you create a Playwright automation script.
Example:
console.log("Launching Browser...");
You would execute the script using:
node loginTest.js
Node.js reads the JavaScript file, executes the code, and displays the output in the terminal.
Best Practices
Use Meaningful File Names
Instead of:
test1.js
Use:
loginTest.js
or
apiValidation.js
Save Before Running
Always save your changes before executing the program.
Keep One Project Per Folder
Organize each project in its own directory.
Example:
JavaScript_Project/
│
├── index.js
├── package.json
└── node_modules/
Practice with Small Programs
Start with simple examples before moving on to advanced topics like functions, objects, and automation frameworks.
Conclusion
Running your first JavaScript program is the first practical step in learning JavaScript. By creating a JavaScript file, writing a simple console.log() statement, and executing it with Node.js, you verify that your development environment is correctly configured.
This basic workflow—writing code, saving the file, and running it from the terminal—is the same process used to develop web applications, backend services, and automation scripts. Mastering this foundation prepares you for more advanced JavaScript concepts and real-world automation projects.
Frequently Asked Questions (FAQs)
What command is used to run a JavaScript file?
Use:
node filename.js
What does console.log() do?
It displays output in the terminal or browser console.
Can JavaScript run without Node.js?
Yes. JavaScript can run inside a web browser. However, Node.js is required to execute JavaScript outside the browser.
Why do I need Node.js for automation testing?
Automation frameworks such as Playwright, Selenium WebDriver (JavaScript), WebdriverIO, and Cypress use Node.js to execute JavaScript test scripts.
Why is my JavaScript program not running?
Common reasons include:
-
Node.js is not installed.
-
The file is not saved.
-
The file name is incorrect.
-
You are running the command from the wrong folder.
Key Takeaways
-
JavaScript files use the
.jsextension. -
console.log()is used to display output. -
Execute JavaScript files using the
nodecommand. -
Save the file before running it.
-
Node.js executes JavaScript outside the browser.
-
The integrated terminal in VS Code simplifies program execution.
-
JavaScript can also run inside web browsers.
-
Proper file organization makes projects easier to manage.
-
Understanding program execution is the foundation for JavaScript development and automation testing.
-
Running your first JavaScript program is the first step toward building real-world applications and automation frameworks.
