Master Node.js From Scratch

Clear, interactive, and structured Node.js lessons designed for beginners and developers.

COURSE SYLLABUS
1. Introduction to Node.js
Introduction to Node.js What Is Node.js? Node.js Architecture Features of Node.js Node.js vs Browser JavaScript Node.js Use Cases
2. Installation and Setup
Installation Introduction LTS and Current Versions npm Setup Environment Setup Verify Installation Node.js REPL
3. Node.js Fundamentals
Fundamentals Introduction Running JavaScript Global Objects Process Object Command-Line Arguments Environment Variables
4. JavaScript Basics
JavaScript Basics Introduction Variables: var, let and const JavaScript Data Types Comparison and Logical Operators If, Else If, Else and Switch Loops and Iteration Functions and Arrow Functions JavaScript Array Methods
5. Node.js Modules
Modules Introduction CommonJS Modules ES Modules Export and Import Built-in Modules Creating Custom Modules Path Module OS Module
6. Working with JSON
JSON Introduction JSON Syntax JSON.stringify() JSON.parse() JSON Array of Objects JSON User Database
7. Node Package Manager
npm Introduction package.json Installing Packages Dependencies and DevDependencies npm Scripts Package Versions and Semantic Versioning npm init and Project Setup
8. File System
File System Introduction Reading Files Writing Files Appending Files Deleting Files Working with Directories
9. CLI Applications
CLI Applications Introduction Command-Line Input Task Manager Project Structure Reading todos.json Adding, Updating and Deleting Tasks Saving Tasks to JSON
10. Events and EventEmitter
Events Introduction EventEmitter Registering Events Emitting Events Removing Event Listeners Custom Events
11. Asynchronous Programming
Async Programming Introduction Callbacks Promises async and await setTimeout() and setInterval() Event Loop and Non-Blocking I/O
12. Native HTTP Module
HTTP Module Introduction Creating an HTTP Server Handling Requests Sending Responses HTTP Request Methods Status Codes and Headers Handling POST Request Body Parsing Incoming JSON Products JSON API
13. Express.js
Express.js Introduction Installing Express Creating an Express Server Request and Response Objects Route Parameters and Query Strings Serving Static Files
14. Express Middleware
Middleware Introduction Built-in Middleware Application-Level Middleware Router-Level Middleware Custom Middleware Error-Handling Middleware
15. REST API Development
REST API Introduction GET Endpoints POST Endpoints PUT and PATCH Endpoints DELETE Endpoints Request Validation and API Responses
16. Database Integration
Database Introduction MySQL Integration MongoDB Integration Database CRUD Operations Database Queries and Filtering Connection Management
17. Authentication and Authorization
Authentication Introduction Password Hashing User Registration User Login JWT Authentication Authorization and Protected Routes
18. Advanced Node.js
Advanced Node.js Introduction Error Handling Debugging Node.js Applications Streams Buffers Performance and Security

Node.js REPL

Learn how to use the Node.js REPL to execute JavaScript statements interactively, perform calculations, declare variables, test functions, and understand the difference between interactive execution and running a JavaScript file.

1. What Is Node.js REPL?

REPL stands for Read, Evaluate, Print, Loop. It is an interactive environment provided by Node.js where developers can enter JavaScript statements and immediately observe their results.

Instead of creating a JavaScript file for every small experiment, we can open the Node.js REPL and try expressions directly. It is useful for learning JavaScript, testing calculations, checking variable values, and experimenting with functions.

Important Concept: REPL is an interactive JavaScript environment. It is different from a browser console because it executes JavaScript using the Node.js runtime.

Meaning of REPL

Term Meaning What Happens?
Read Accepts your input. You enter a JavaScript expression.
Evaluate Processes the input. Node.js evaluates the expression.
Print Displays the result. The evaluated value appears in the terminal.
Loop Waits for more input. You can enter another statement.

2. How Node.js REPL Works

The REPL repeatedly reads a statement, evaluates it, displays the result, and waits for the next statement.

Read
Enter a JavaScript expression
↓
Evaluate
Node.js processes the expression
↓
Print
Display the result
↓
Loop
Wait for the next expression
↻
Enter another JavaScript statement

This cycle continues until you exit the REPL or close the terminal.

3. How to Start Node.js REPL

Before starting REPL, ensure that Node.js is installed and available in your terminal.

Step 1: Open the Terminal

Open Command Prompt, PowerShell, or your code editor's terminal.

Step 2: Execute the Node Command

Terminal
node

When you run node without specifying a JavaScript filename, Node.js normally opens the interactive REPL.

Example Terminal Session

Node.js REPL
Welcome to Node.js.
Type ".help" for more information.
>

The > prompt indicates that REPL is ready to accept JavaScript input. The welcome message and version details may vary depending on your Node.js version.

Remember: Type JavaScript expressions after the REPL prompt. Do not type the prompt character itself.

4. Execute Basic JavaScript Expressions

The simplest way to understand REPL is to enter expressions and immediately inspect their returned values.

Example 1: Addition

REPL Input
> 10 + 20
Output
30

Node.js evaluates the addition expression and displays its result.

Example 2: Multiplication

REPL Input
> 12 * 5
Output
60

Example 3: String Concatenation

REPL Input
> "CIIT" + " Training Institute"
Output
'CIIT Training Institute'

String concatenation combines strings into a single string. REPL displays the resulting value directly.

5. Use Variables in REPL

Variables allow us to store values and use them in later expressions. We can declare variables in REPL just as we do in a JavaScript file.

Example: Declare a Variable

REPL Input
> let course = "Node.js"
> course
Output for the Second Expression
'Node.js'

The first statement declares the variable. The second expression evaluates the variable name and displays its value.

Example: Perform a Calculation

REPL Input
> let price = 500
> let quantity = 3
> price * quantity
Output for the Calculation
1500

The expression multiplies the stored price by the quantity and returns the total.

Important: REPL maintains an interactive session. If you declare a variable and then declare the same lexical variable again with let or const, you may get an identifier-already-declared error. Use a different name or restart the session when appropriate.

6. Test JavaScript Functions in REPL

We can define a function and call it immediately in REPL. This is useful for testing small pieces of JavaScript logic.

Example: Addition Function

REPL Input
> function add(a, b) {
...   return a + b;
... }
undefined
> add(15, 25)
40

Explanation

  • function add(a, b) declares a function.
  • a and b are function parameters.
  • return a + b returns the sum.
  • add(15, 25) calls the function with two values.
About the Output: The value undefined after the function definition indicates that the declaration itself did not return a value. The subsequent function call returns 40.

7. Test Built-in JavaScript Methods

REPL is also useful for checking built-in JavaScript methods without creating a separate file.

Example 1: Convert Text to Uppercase

REPL Input
> "node.js".toUpperCase()
Output
'NODE.JS'

Example 2: Find String Length

REPL Input
> "JavaScript".length
Output
10

Example 3: Work with Arrays

REPL Input
> let marks = [70, 80, 90]
> marks.length
Output for the Second Expression
3

These examples demonstrate how REPL helps us test strings, arrays, mathematical expressions, and other JavaScript features.

8. Useful Node.js REPL Commands

REPL provides special commands that help us get information, inspect the current session, and exit the environment.

Command Purpose
.help Displays help information about REPL commands.
.break Attempts to exit a multiline expression.
.clear Resets the REPL context.
.save filename.js Saves the current REPL session input to a file.
.load filename.js Loads and executes a JavaScript file in the REPL context.
.exit Exits the REPL session.
Note: These commands are REPL commands, not ordinary JavaScript statements. Their availability and exact behavior can depend on the Node.js version and REPL environment.

9. How to Exit Node.js REPL

Once you finish experimenting, you can exit the interactive session and return to the normal terminal.

Method 1: Use the Exit Command

REPL Input
> .exit

Method 2: Use the Keyboard Shortcut

On many terminals, pressing Ctrl + C twice or using Ctrl + D on supported terminals can exit the REPL. The exact behavior depends on the terminal and operating system.

Start REPL using node
↓
Execute expressions and test code
↓
Enter .exit
↓
Return to the normal terminal

10. Node.js REPL vs JavaScript File

REPL and JavaScript files both execute code through Node.js, but they are useful in different situations.

Feature Node.js REPL JavaScript File
Execution Interactive, one input at a time. Executes code from a saved file.
Starting Command node node app.js
Best Use Quick experiments and testing expressions. Reusable programs and applications.
Code Storage Session history may be available. Code is stored in a file.
Application Development Useful for small experiments. Better suited to structured projects.
Practical Advice: Use REPL to understand and test small expressions. Use JavaScript files when building applications that need to be saved, maintained, tested, and shared.

11. CIIT Training Institute - Practice Task

Task: Practice JavaScript Using REPL

Open Node.js REPL and complete the following activities:

  1. Start REPL using the node command.
  2. Calculate the sum of 100 and 200.
  3. Create a variable named studentName.
  4. Store a course name in another variable.
  5. Print the length of a string.
  6. Create an array containing three marks.
  7. Check the number of elements in the array.
  8. Create a function that multiplies two numbers.
  9. Call the function and observe the result.
  10. Exit REPL using .exit.
Sample Practice Expressions
REPL Input
100 + 200

let studentName = "Student"
let courseName = "Node.js"

studentName
courseName

"CIIT Training Institute".length

let marks = [75, 85, 95]
marks.length

function multiply(a, b) {
    return a * b;
}

multiply(5, 6)
Expected Results
300
'Student'
'Node.js'
25
3
30

The expressions are evaluated one at a time. The displayed values correspond to expressions that return results; variable declarations and function definitions may display undefined or no separate value.

Summary

Node.js REPL provides an interactive environment for executing JavaScript expressions and observing results immediately. We learned the Read, Evaluate, Print, Loop process, how to work with variables and functions, test built-in methods, use REPL commands, and exit the session. REPL is useful for quick experiments, while JavaScript files are better suited to reusable applications. Practical exercises at CIIT TRAINING INSTITUTE help students strengthen their JavaScript fundamentals and prepare for Node.js development.