JavaScript Essentials for Node
The core JavaScript you need before you build with Node.js
Big Idea: Node.js lets you run JavaScript on your computer, outside the browser. Before you build servers and tools, you need a solid grip on the building blocks of the language: values, variables, functions, objects, and arrays. Master these and the rest of Node feels easy.
By the end you will be able to:
- declare variables with
letandconstand know which to pick, - name the primitive types and check them with
typeof, - build text with template strings,
- write functions and arrow functions,
- store data in objects and arrays and read it back.
1. How Node runs your code
You write JavaScript in a file that ends in .js. Then you ask Node to run it from your terminal:
node app.js
Node reads your file top to bottom and does exactly what it says. Anything you console.log(...) is printed in the terminal.
2. Variables: let and const
A variable is a labelled box that holds a value. In modern JavaScript you make one with let or const.
let— use it when the value will change later (a score, a counter).const— use it when the value should stay the same (a name, a setting). This is the safer default.
let score = 0; // can change later
const name = 'Ada'; // stays the same
score = 10; // OK, let can be reassigned
console.log(score); // 10
console.log(name); // Ada
// name = 'Bob'; // TypeError: Assignment to constant variable.
Analogy: Think of a variable as a labelled jar.
let is a jar you can empty and refill. const is a jar with the lid glued on — you cannot swap what it points to. Reach for const first, and switch to let only when you truly need to reassign.