Explain it simply
Explain it simply
JavaScript is the programming language that gives web pages behaviour. HTML decides what is on the page and CSS decides how it looks, but when a menu opens, a form warns you about a missing email, or a chat message appears without a reload, JavaScript is doing that work.
The same language also runs outside the browser. With Node.js you can run a .js file from a terminal to build servers, command-line tools and scripts. This course teaches the language first, so most examples run in both places.
Problem → Why → Solution
The problem: the same code works in one place and crashes in another
Many beginners copy a snippet from a web tutorial, save it as app.js and run node app.js. It crashes with ReferenceError: document is not defined, even though the same lines worked in the browser console a minute ago. Others try a Node.js example in the browser and see require is not defined. It feels as if JavaScript itself is broken or inconsistent.
// app.js - fine in a browser console, crashes in Node.js
const heading = document.querySelector("h1");
heading.textContent = "Welcome!";
// $ node app.js
// ReferenceError: document is not definedWhy does this happen?
JavaScript the language and the place that runs it are two different things. Language features such as variables, functions, arrays and objects exist everywhere. The host adds extra tools on top: a browser adds document and window for the page, while Node.js adds process and modules such as fs for files. Code that uses a host tool only works in that host.
Core concept: an engine plus a host
Every JavaScript program needs two pieces: an engine that understands the language, and a host environment that connects the program to the outside world. The engine reads your code, turns it into fast machine instructions while it runs, and manages memory. Chrome, Edge and Node.js use the V8 engine; Firefox uses SpiderMonkey and Safari uses JavaScriptCore.
The language rules every engine follows are written down in a standard called ECMAScript. That is why an if statement behaves identically wherever it runs.
The host decides what your code can touch. In a browser, the host gives you document (the whole page as objects you can change), window, fetch for network requests, and events such as clicks. Node.js has no page, so those are missing; instead you get process (the running program and its arguments) and modules such as fs to read and write files and http to build a server. Both hosts provide console.log and timers such as setTimeout.
| Part | Browser | Node.js |
|---|---|---|
| Engine | V8, SpiderMonkey, JavaScriptCore | V8 |
| Host objects | document, window, localStorage | process, fs, http |
| How you run code | DevTools Console or a <script> tag | node file.js in a terminal |
| Typical use | Interactive pages | Servers, tools, scripts |
To run code in a browser, open any page, press F12 (Cmd+Option+J on a Mac) and type into the Console tab. To run it with Node.js, install Node.js from nodejs.org, save the code as hello.js and type node hello.js in a terminal. Everything console.log() prints appears in that console or terminal. Until the DOM lessons the course is pure language, so either option works; the DOM lessons need a browser. If an example in a later lesson prints something different on your machine, first check that you ran it in the host the lesson expected.
- You write JavaScript code
- An engine such as V8 executes it
- The host adds document or fs
- Output appears in the console
Language features work everywhere; host features only work in their own host.
General pattern: the shape of a small program
// 1. store values under names
const name = value;
// 2. compute new values from them
const result = name + otherValue;
// 3. show the result
console.log("label:", result);Most small programs follow these three steps: store values under names, compute new values from them, and show the result with console.log(). Give console.log() several comma-separated arguments and it prints them separated by spaces.
Example: your first program
Save this as hello.js and run node hello.js, or paste it into the browser console. The program stores values in variables, builds text from them and finally checks which host is running it. Predict each printed line before you look at the output.
// hello.js - run it with: node hello.js
const language = "JavaScript";
const places = ["the browser", "Node.js"];
console.log(`Hello from ${language}!`);
console.log(`The same language runs in ${places.join(" and ")}.`);
console.log("2 + 3 =", 2 + 3);
const host = typeof window === "undefined" ? "Node.js" : "a browser";
console.log(`Right now it is running in ${host}.`);Code breakdown
- Line 2
const language = "JavaScript";const stores a value under a name. Here the text (a string) "JavaScript" is stored under the name language.
- Line 3
const places = ["the browser", "Node.js"];Square brackets [ ] create an array, an ordered list of values. Line 6 uses places.join(" and ") to glue the two items into one string with " and " between them.
- Line 5
console.log(`Hello from ${language}!`);Text written between backticks (`) is a template literal. The value of the variable replaces ${language}, so it prints Hello from JavaScript!.
- Line 7
console.log("2 + 3 =", 2 + 3);There are two arguments. The first is inside quotes, so it prints as written; the second, 2 + 3, is calculated first and prints as 5. console.log() puts a space between them.
- Line 9
const host = typeof window === "undefined" ? "Node.js" : "a browser";window only exists in browsers. In Node.js, typeof window is "undefined", so host becomes "Node.js". Run the same code in a browser console and the last line becomes Right now it is running in a browser.
Hello from JavaScript!
The same language runs in the browser and Node.js.
2 + 3 = 5
Right now it is running in Node.js.How this course is organised
| Chapter | Lessons | What you learn |
|---|---|---|
| Basic | 10 | Syntax, variables, data types, operators, if/else, switch, loops |
| Intermediate | 17 | Functions, objects, arrays, strings, classes, errors, debugging, the DOM |
| Advanced | 17 | Closures, scope, async code, promises, fetch, APIs, JSON, ES6+, modules |
| Projects | 5 | Calculator, to-do list, weather app, quiz app, API dashboard |
| Exercises | 2 | Mixed practice sets for fundamentals and advanced challenges |
Every lesson follows the same rhythm: a plain-language explanation, a real problem, the concept, a runnable example, common mistakes, a mini challenge and a short quiz. Attempt each mini challenge before you open its solution. Struggling a little in front of an empty editor teaches far more than reading a finished answer.
In real projects
Where this shows up in real projects
Because one language runs on both sides, a single team can write a shop's interactive front end, the server that stores orders, and the build scripts that bundle everything, all in JavaScript. Frameworks such as React, Vue and Angular run in the browser; libraries such as Express run on Node.js. Even desktop apps like VS Code are built on a JavaScript runtime through Electron.
In daily work, developers live in two consoles: the browser DevTools console to inspect a page and test a quick idea, and a terminal running node for scripts and servers. Reading the error message in either one, especially the file name and line number it points to, is the skill that saves the most time in real projects.
Common mistakes
Common mistakes
1. Running browser code in Node.js (or the reverse)
If a snippet uses document, window, alert or localStorage, it needs a browser; if it uses require, process or fs, it needs Node.js. Check which host an example targets before running it, and read ReferenceError: X is not defined as a hint that X may belong to the other host.
2. Confusing JavaScript with Java
Despite the similar name, Java is a separate language with its own compiler, types and runtime. Java code from search results will not run in a JavaScript console, so put the word JavaScript (or JS) in every search.
3. Reading examples instead of running them
Reading a program and predicting its output feels like understanding, but you only find the gaps when you type it, run it and change it. In every example, change one value and predict the new output before you press Enter.
Best practices
Best practices
- Keep one folder for the course and one file per lesson, such as 04-variables.js, so you can re-run old examples.
- Install the current LTS version of Node.js rather than an odd-numbered release.
- Use console.log() generously while learning; it is the fastest way to see what a value really is.
Mini challenge
Mini challenge: introduce yourself in code
Write a small program, about-me.js, that prints a short introduction card built from variables, and run it with node about-me.js.
- Store your name, a learning goal and the minutes you can study per day in const variables
- Print a greeting and the goal using template literals
- Calculate and print the study hours per week (minutes * 7 / 60)
- Print which host is running the file
// TODO: replace these placeholder values with your own
const name = "";
const goal = "";
const minutesPerDay = 0;
// TODO: calculate hours per week from minutesPerDay
// TODO: print "Hi, I am <name>."
// TODO: print "My goal: <goal>."
// TODO: print "Study time: <hours> hours per week"
// TODO: print "Runtime: Node.js" or "Runtime: browser"Expected: With the sample values it prints: Hi, I am Aye Aye. My goal: build a to-do app. Study time: 3.5 hours per week Runtime: Node.js
Show hint
Use backticks for template literals: `Hi, I am ${name}.`. To detect the host, use typeof window === "undefined", which is true in Node.js.
Show solution
const name = "Aye Aye";
const goal = "build a to-do app";
const minutesPerDay = 30;
const hoursPerWeek = (minutesPerDay * 7) / 60;
console.log(`Hi, I am ${name}.`);
console.log(`My goal: ${goal}.`);
console.log(`Study time: ${hoursPerWeek} hours per week`);
console.log(typeof window === "undefined" ? "Runtime: Node.js" : "Runtime: browser");Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- JavaScript gives web pages behaviour, and with Node.js it also runs outside the browser.
- An engine such as V8 runs the language; the host (browser or Node.js) adds tools such as document or fs.
- Open the Console with F12 in a browser, or run node file.js in a terminal.
- The course has five chapters: Basic, Intermediate, Advanced, Projects and Exercises.
MDN — JavaScript Guide — MDN Web Docs