JavaScript Prerequisites for Node.js

๐ŸŸข Node.js LTS ๐Ÿ“— Chapter 2 of 49 ๐Ÿ“‚ Phase 1: Node.js Introduction ๐Ÿ—“๏ธ 2026 Edition
๐Ÿ“Œ Covered in this chapter: const/let ยท Arrow Functions ยท Destructuring & Spread ยท Callbacks ยท Promises ยท async/await ยท JSON

A focused refresher on the modern JavaScript (ES6+) features that every Node.js program leans on: variable declarations, arrow functions, destructuring, the spread operator, and the three async patterns โ€” callbacks, Promises, and async/await โ€” plus JSON serialization basics.

1Variables, Data Types & Template Literals

Before writing Node.js programs, you need to be comfortable with modern JavaScript (ES6+) syntax, since almost all Node.js code โ€” and every example in this course โ€” is written using it. Modern JavaScript prefers const and let over the older var keyword.

  • const โ€” declares a variable that cannot be reassigned. Use this by default.
  • let โ€” declares a variable that can be reassigned later. Use this only when the value needs to change.
  • var โ€” the old, function-scoped way of declaring variables. Avoid it in new Node.js code.

JavaScript's core data types are: string, number, boolean, null, undefined, object, and symbol. Template literals (backtick strings) let you embed expressions directly inside text using ${'{'}...{'}'}.

๐Ÿ’ป Example 1: const, let & Template Literals
const courseName = "Node.js Master Course";
let studentsEnrolled = 120;

studentsEnrolled = studentsEnrolled + 1;

console.log(`${courseName} now has ${studentsEnrolled} students.`);
๐Ÿ” Line-by-Line Code Breakdown:
  • const courseName: Locked โ€” trying to reassign it later would throw a TypeError.
  • let studentsEnrolled: Mutable, so incrementing it on the next line is valid.
  • The template literal `${courseName}...` interpolates both variables directly into the string without manual concatenation.
2Functions, Arrow Functions & Default Parameters

Functions are the core building block of any Node.js application โ€” every route handler, callback, and middleware is a function. Modern JavaScript offers a concise arrow function syntax alongside the traditional function keyword.

Arrow functions do not bind their own this โ€” they inherit it from the surrounding scope, which is why they're preferred inside callbacks and class methods where losing the outer this is a common bug.

๐Ÿ’ป Example 2: Regular Function vs Arrow Function
// Traditional function declaration
function add(a, b) {
  return a + b;
}

// Arrow function with a default parameter
const greet = (name = "Developer") => {
  return `Hello, ${name}! Welcome to Node.js.`;
};

console.log(add(5, 7));
console.log(greet());
console.log(greet("Balaji"));
๐Ÿ” Line-by-Line Code Breakdown:
  • function add(a, b): A traditional named function that returns the sum of its two arguments.
  • (name = "Developer") =>: An arrow function with a default parameter โ€” if no argument is passed, name falls back to "Developer".
  • greet() vs greet("Balaji"): Shows the default parameter kicking in only when the argument is omitted.
3Arrays, Objects, Destructuring & the Spread Operator

Node.js APIs constantly hand you back arrays and objects โ€” HTTP headers, query parameters, database rows, JSON responses. Being fluent with array/object syntax and destructuring is essential.

  • Destructuring extracts values from arrays or objects into individual variables in one line.
  • Spread (...) expands an array or object's contents โ€” useful for copying, merging, and passing multiple arguments.
๐Ÿ’ป Example 3: Destructuring & Spread in Action
const course = {
  title: "Node.js Master Course",
  level: "Beginner to Advanced",
  chapters: 49,
};

// Object destructuring
const { title, chapters } = course;
console.log(title, "-", chapters, "chapters");

// Spread to create a new object with an extra field
const updatedCourse = { ...course, edition: 2026 };
console.log(updatedCourse);

// Array destructuring
const [first, second] = ["Node.js", "Express.js"];
console.log(first, second);
๐Ÿ” Line-by-Line Code Breakdown:
  • const { title, chapters } = course: Pulls just those two properties out of the object into standalone variables.
  • { ...course, edition: 2026 }: Spreads all of course's existing keys into a brand-new object, then adds (or overrides) edition.
  • const [first, second] = [...]: Array destructuring assigns items by position.
4Callbacks, Promises & async/await

Because Node.js is asynchronous by nature, you'll write asynchronous JavaScript constantly. There are three patterns for handling async code, and Node.js code today typically uses all three depending on the API:

  • Callbacks: a function passed as an argument, invoked later once an operation finishes.
  • Promises: objects representing a value that will be available eventually โ€” either resolved (success) or rejected (failure).
  • async/await: syntax sugar built on top of Promises that lets asynchronous code read like synchronous code.
๐Ÿ’ป Example 4: The Same Task, Three Ways
// 1. Promise-based helper that "simulates" an async task
function delay(ms) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

// 2. async/await โ€” the modern, preferred style
async function loadCourse() {
  console.log("Loading course data...");
  await delay(1000);
  console.log("Course data loaded!");
}

loadCourse();
๐Ÿ” Line-by-Line Code Breakdown:
  • new Promise((resolve) => ...): Creates a Promise that resolves after setTimeout fires.
  • async function loadCourse(): Marking a function async lets you use await inside it.
  • await delay(1000): Pauses this function only (not the whole program) until the Promise resolves, then continues.
5Modules, JSON & HTTP Basics You'll Reuse Everywhere

Two more building blocks you'll use constantly in Node.js: modules (splitting code across files) and JSON (the data format almost every API speaks).

JavaScript's built-in JSON.stringify() converts a JavaScript object into a JSON string (for sending over the network or writing to a file), and JSON.parse() converts a JSON string back into a usable JavaScript object.

You'll also frequently deal with basic HTTP concepts โ€” requests, responses, status codes (like 200 OK, 404 Not Found), and headers โ€” since Node.js is most often used to build HTTP servers and API clients (covered fully in Phase 6).

๐Ÿ’ป Example 5: JSON.stringify() and JSON.parse()
const course = { title: "Node.js Master Course", chapters: 49 };

// Convert JS object -> JSON string (e.g. to send over the network)
const jsonString = JSON.stringify(course);
console.log(typeof jsonString, jsonString);

// Convert JSON string -> JS object (e.g. after receiving an API response)
const parsedBack = JSON.parse(jsonString);
console.log(typeof parsedBack, parsedBack.title);
๐Ÿ” Line-by-Line Code Breakdown:
  • JSON.stringify(course): Serializes the object into a plain string โ€” this is exactly what an Express route does before sending a JSON API response.
  • JSON.parse(jsonString): Deserializes the string back into a real JavaScript object with usable properties.
โ“ Frequently Asked Questions (FAQ)

Q Do I need to master JavaScript fully before starting Node.js?

Not fully, but you should be comfortable with variables, functions, arrays/objects, and basic async patterns (Promises and async/await) โ€” this course reinforces them as they come up, but strong JS fundamentals make Node.js much easier to pick up.

Q What's the difference between var, let, and const?

var is function-scoped and can be redeclared, which causes subtle bugs; let and const are block-scoped. Use const by default and let only when a variable's value needs to change; avoid var in modern code.

Q Why does Node.js prefer async/await over plain callbacks?

async/await, built on Promises, avoids deeply nested 'callback hell', supports try/catch for error handling, and makes asynchronous logic read top-to-bottom like synchronous code โ€” dramatically improving readability in real Node.js applications.

OC
Written by Our Compiler Technical Editorial Team
Reviewed for accuracy & tested on Node.js LTS ยท Last updated August 2026