Setup & First Program
Write your first JavaScript hello world console program. Set up browser DevTools, VS Code and Node.js step by step.
On this page
1. What are setup and the first program?
There are three easy ways to run JavaScript:
- Browser console, nothing to install.
- A
scripttag inside an HTML file. - Node.js, running a file from the terminal.
VS Code is the most common editor for writing code.
2. Why does it matter?
While learning, the most important thing is to write code, run it and see the result right away. A good setup makes this cycle fast.
| Without a proper setup | With a proper setup |
|---|---|
| Code in Notepad, no help | Autocomplete and error hints in VS Code |
| Errors go unnoticed | The console shows the error and line number |
| Hunting for the page for every test | One command in the terminal to run |
| Guessing the output | Seeing every value with console.log |
3. Where is it used?
| Method | When to use it |
|---|---|
| Browser console | When you want to test something small right away |
| HTML + script file | When you are working with a web page |
| Node.js | When you are building scripts, a backend or tools |
| VS Code | For writing any kind of code |
4. How does it work?
Opening the browser console: In Chrome, right click on the page, choose Inspect, then open the Console tab. Shortcut: Ctrl + Shift + J on Windows, Cmd + Option + J on Mac.
Installing Node.js: Install the LTS version from nodejs.org. Then check the version in the terminal:
node --version
npm --version5. Code example
Method 1: Browser console. Type this in the console and press Enter:
console.log("Hello, World!");Method 2: HTML file. Create index.html and app.js in a folder:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>My first program</title>
<script src="app.js" defer></script>
</head>
<body>
<h1>I am learning JavaScript</h1>
</body>
</html>const name = "Ayesha";
console.log("Hello, World!");
console.log(`My name is ${name}`);Open index.html in the browser and check the console.
Method 3: Node.js. Run the same app.js from the terminal:
node app.jsHow does the code work?
| Item | Meaning |
|---|---|
script src="app.js" | Tells the browser to load this JavaScript file |
defer | The script runs after the HTML is fully parsed |
console.log("...") | The text is printed to the console |
Backticks and ${name} | A template literal, which inserts a variable into text |
node app.js | Node.js runs the file in the terminal |
Interview tip
The difference between defer and async is asked often. Scripts with defer run in their own order after the HTML is parsed. Scripts with async run as soon as they are downloaded, and the order is not fixed.
6. Common mistakes
Mistake: Putting the script in the head without defer
Why? The script runs before the HTML, the elements do not exist yet, so querySelector returns null.
Better: Add defer to the script tag or use type module, both run after the HTML.
Mistake: Writing the file path wrong
Why? The browser cannot find the file and the console shows a 404 error, so the code never runs.
Better: Check the Network tab or the console and fix the src path relative to the HTML file.
Mistake: Forgetting to open the console
Why? The output of console.log appears in the DevTools console, not on the page. It looks like the code is not working.
Better: Keep DevTools open while you work and read errors right away.
7. What to learn before and after
| Stage | Topics |
|---|---|
| Before | Introduction, How JavaScript Runs |
| After this | Variables, Data Types |
| Related | Debugging JavaScript, npm & Packages |
Final quick revision
| Point | Detail |
|---|---|
| Editor | VS Code |
| Quick test | Browser DevTools console |
| With a web page | script src + defer |
| From the terminal | node app.js |
| Seeing output | console.log() |