Write Hello World in VS Code
Introduction
This chapter creates a small JavaScript project in Visual Studio Code, writes a Hello World script, and runs it from the integrated terminal. You will practice the folder-based workflow used in real repos instead of single loose files on the desktop.
Prerequisites
- VS Code installed (see Install a Code Editor)
- Node.js LTS on PATH (
node --version) - Completed terminal Hello World (optional but helpful)
Step 1: Create a Project Folder
Pick a location you can find later, for example:
~/projects/js-hello (macOS/Linux)
C:\Users\You\projects\js-hello (Windows)In VS Code:
- File → Open Folder… (or Open… on macOS)
- Select
js-hello(create the folder first in your file manager if needed) - Trust the folder if VS Code asks (for local learning folders, trusting is normal)
Step 2: Add hello.js
- In the Explorer sidebar, click New File
- Name the file
hello.js - Add the following code:
// Declare a constant string for the topic name (const means it cannot be reassigned)
const topic = "JavaScript";
// Template literal: backticks allow embedding variables with ${...}
console.log(`Hello, ${topic}!`);
// A plain string message printed on the second line of output
console.log("Running from VS Code terminal.");- Save the file (
Ctrl+S/Cmd+S)
You should see syntax highlighting for strings, const, and template literals.
Step 3: Run from the Integrated Terminal
- Open Terminal → New Terminal
- Confirm the shell prompt is inside your project folder
- Run:
# Execute hello.js with Node.js
node hello.jsExpected output:
Hello, JavaScript!
Running from VS Code terminal.Tip
Best Practice
Keep one terminal per task window when learning. If output looks wrong, check the terminal’s current directory with pwd (macOS/Linux) or cd (Windows).
Step 4: Optional Run Configuration (Debug Panel)
For later debugging practice, you can add a minimal launch config.
- Open the Run and Debug view (
Ctrl+Shift+D/Cmd+Shift+D) - Click create a launch.json file
- Choose Node.js environment
- VS Code may generate:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Run hello.js",
"program": "${workspaceFolder}/hello.js"
}
]
}- Press F5 to run with the debugger attached (breakpoints work when you add them later)
For Hello World, node hello.js in the terminal is enough. Launch configs become valuable as scripts grow.
Step 5: Optional package.json (Preview)
Real JavaScript projects usually include a package.json manifest. You do not need it for Hello World, but creating one early is good habit:
# Create a default package.json in the project folder
npm init -yThis command registers the project name and version so later chapters can add dependencies and npm scripts cleanly.
Common Issues
| Symptom | Likely cause | Fix |
|---|---|---|
node not found in VS Code terminal | PATH differs from system terminal | Restart VS Code; install Node.js; on Windows, reboot after installer |
| File not found | Terminal not in project root | cd to folder containing hello.js |
| No syntax highlighting | File not saved as .js | Rename to hello.js and save |
FAQ
Should I open a file or a folder in VS Code?
Open a folder for projects. Single-file mode is fine for tiny experiments, but folders match how teams organize code.
What is the difference between Run and Debug?
Run (node hello.js) executes immediately. Debug (F5) attaches the debugger so you can pause on lines and inspect variables.
Do I need package.json for Hello World?
No. Add it when you install packages or define npm scripts—soon in later engineering chapters.
Can I use WebStorm instead of VS Code?
Yes. Create the same hello.js, open the project folder, and use WebStorm’s terminal to run node hello.js.
Why use template literals (`Hello, ${topic}!`)?
Template literals (ES6) make string interpolation readable. You will learn strings in depth in later chapters.