Node.js Syntax

Node executes JavaScript syntax. The language rules are the same in browser and server code, but the runtime APIs are different.

Statements and values

Node.js runs on the V8 JavaScript engine, supporting modern ECMAScript syntax out of the box. Declare bindings with const by default so references cannot be accidentally reassigned, and use let only for counters or accumulators that genuinely change. Avoid legacy var because its function-scoping and hoisting cause subtle bugs:

const appName = "node-lab";   // block-scoped constant binding
let count = 1;                // block-scoped mutable binding
count += 1;
console.log(`${appName}: count=${count}`);

Language rules

Consistent syntax conventions make server codebases easier to review and prevent Automatic Semicolon Insertion (ASI) surprises:

  • Strict equality: Always use === and !== instead of == to avoid implicit type coercion.
  • Default to const: Remember that const freezes the variable binding, not the properties inside an object or array.
  • Consistent formatting: Use Prettier or ESLint so semicolon and quote styles stay uniform across all contributors.

Source structure

Keep top-level entry scripts focused on configuration, wiring, and process lifecycle (reading environment variables, starting the server, handling shutdown signals). Move pure business logic into small, testable modules, and isolate filesystem or network I/O at the edges.

Practice

Run a short script with node to verify bindings and equality rules:

  1. Write a script that prints formatted output with template literals (`${...}`).
  2. Mutate a property on a const object, then try reassigning the variable itself and observe the TypeError.
  3. Compare "5" == 5 versus "5" === 5 in the Node REPL.