JavaScript Requirements
Node.js is "just JavaScript", but real Node code leans heavily on a handful of modern language features. This lesson is a quick refresher. If everything here looks familiar, you are ready for the rest of the course.
Variables: const and let
Use const by default and let when a value must change. Avoid the old var.
const course = "Node.js";
let lessons = 10;
lessons += 30;
console.log(course, lessons);Node.js 40
Arrow functions and template literals
Arrow functions are short and are used everywhere as callbacks. Template literals (backticks) embed values with ${...}.
const square = (n) => n * n;
const greet = (name = "world") => `Hello, ${name}!`;
console.log(square(7));
console.log(greet());
console.log(greet("Node"));49
Hello, world!
Hello, Node!
Objects, destructuring and spread
Node APIs often return objects, and destructuring pulls out just the parts you need. Spread (...) copies or merges.
const config = { port: 3000, host: "localhost", debug: false };
const { port, host } = config;
console.log(`${host}:${port}`);
const devConfig = { ...config, debug: true };
console.log(devConfig);
const [first, ...rest] = ["a", "b", "c"];
console.log(first, rest);localhost:3000
{ port: 3000, host: 'localhost', debug: true }
a [ 'b', 'c' ]
Array methods
map, filter, reduce and find replace most hand-written loops.
const orders = [
{ id: 1, total: 25, paid: true },
{ id: 2, total: 40, paid: false },
{ id: 3, total: 15, paid: true },
];
const paid = orders.filter((o) => o.paid);
const ids = paid.map((o) => o.id);
const revenue = paid.reduce((sum, o) => sum + o.total, 0);
console.log(ids, revenue);
console.log(orders.find((o) => o.total > 30));[ 1, 3 ] 40
{ id: 2, total: 40, paid: false }
Callbacks, promises and async/await
Node does slow work (files, network) asynchronously. Older APIs take a callback; modern APIs return a promise, which you consume with await inside an async function (or at the top level of an ES module).
const wait = (ms, value) =>
new Promise((resolve) => setTimeout(() => resolve(value), ms));
async function main() {
console.log("waiting...");
const result = await wait(10, "done");
console.log(result);
const both = await Promise.all([wait(5, "A"), wait(1, "B")]);
console.log(both);
}
main();waiting...
done
[ 'A', 'B' ]
Promise.all keeps the results in the original order, even though "B" finished first.
Checklist
| Feature | Why Node code needs it |
|---|---|
const / let |
block scoping, fewer bugs |
| arrow functions | callbacks for events, timers and array methods |
| template literals | building strings, paths, HTML and log lines |
| destructuring | const { readFile } = require("node:fs/promises") |
| spread / rest | merging config objects, forwarding arguments |
| classes | class MyEmitter extends EventEmitter {} |
promises / async / await |
nearly every I/O API |
try / catch |
handling errors from await |
| modules | require / import to split code into files |
Tip: If any of these feel shaky, work through the JavaScript course first. Node will feel much easier.
Test yourself
What does const { port } = config; do?
- Copies the whole object
- Creates a variable
portfromconfig.port - Renames
configtoport - Throws unless
portis a function
Where can you use the await keyword?
- Anywhere in any file
- Only inside
asyncfunctions or at the top level of an ES module - Only inside callbacks
- Only in the browser
What does Promise.all([p1, p2]) resolve to?
- The first value to finish
- An array of results in the same order as the input
- An array in completion order
undefined