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

JavaScript Variables: var, let and const

Learn how to declare variables in JavaScript using var, let, and const. Understand reassignment, scope, and the differences between these keywords with practical examples.

1. What Is a Variable?

A variable is a named identifier used to store or refer to a value in a program. We can use variables to store names, numbers, prices, results, and other application data.

JavaScript provides three common keywords for declaring variables: var, let, and const.

Example

JavaScript
let studentName = "Samadhan";
let age = 22;

console.log(studentName);
console.log(age);

Output

Output
Samadhan
22

Explanation: The variable studentName stores a string, while age stores a number. The console.log() function displays their values.

2. Variable Declaration Diagram

A variable declaration introduces a variable name. An assignment provides a value to that variable.

Declaration Keyword

let, const, or var

↓
Variable Name

studentName

↓
Assigned Value

"Samadhan"

Complete Statement

JavaScript
let studentName = "Samadhan";

Here, let declares the variable, studentName is the variable name, and "Samadhan" is the assigned value.

3. What Is var?

The var keyword is used to declare a variable. A var variable is function-scoped or globally scoped when declared outside a function. It is not limited to a block such as an if statement or loop.

Example

JavaScript
var course = "JavaScript";

console.log(course);

course = "Node.js";

console.log(course);

Output

Output
JavaScript
Node.js

Explanation: We first assign "JavaScript" to the variable. Later, we assign "Node.js" to the same variable. Reassignment is allowed with var.

4. What Is let?

The let keyword declares a block-scoped variable. Its value can be reassigned, but the same variable cannot be declared again in the same scope.

Example

JavaScript
let marks = 70;

console.log(marks);

marks = 85;

console.log(marks);

Output

Output
70
85

Explanation: The variable marks initially stores 70. We then update it to 85. Since let allows reassignment, the program runs successfully.

Block Scope Example

JavaScript
if (true) {
    let message = "Inside the block";
    console.log(message);
}

// message is not accessible here

Output

Output
Inside the block

The variable message is accessible inside the if block only. Trying to access it outside the block would cause a ReferenceError.

5. What Is const?

The const keyword declares a block-scoped binding that must be initialized when declared. The binding cannot be reassigned later.

Example

JavaScript
const instituteName = "CIIT Training Institute";

console.log(instituteName);

Output

Output
CIIT Training Institute

Explanation: The variable is initialized when declared. We can read its value, but we cannot assign a different value to the same binding.

Invalid Example

JavaScript
const course = "JavaScript";

course = "Node.js";

Result

Error
TypeError: Assignment to constant variable.

Note: A const object or array can still have its contents modified. The restriction applies to reassignment of the binding, not automatic immutability of the object.

6. Difference Between var, let and const

Feature var let const
Scope Function or global Block Block
Reassignment Allowed Allowed Not allowed
Redeclaration in the same scope Allowed Not allowed Not allowed
Must initialize at declaration No No Yes
Recommended for new code Generally avoid When reassignment is needed When reassignment is not needed

7. Understanding Variable Scope

Scope determines where a variable can be accessed in a program. The following example demonstrates the difference between function scope and block scope.

Function Scope

var is accessible throughout its containing function.

↓
Block Scope

let and const are accessible within their declaring block.

Example

JavaScript
if (true) {
    var first = "var variable";
    let second = "let variable";

    console.log(first);
    console.log(second);
}

console.log(first);

// second is not accessible outside the block

Output

Output
var variable
let variable
var variable

The var variable remains accessible outside the if block because it is not block-scoped. The let variable is limited to the block. Uncommenting a statement that accesses second outside the block would cause a ReferenceError.

8. Best Practices

  • Prefer const when a variable binding does not need to be reassigned.
  • Use let when the variable needs reassignment.
  • Avoid using var in new code unless its behavior is specifically required.
  • Choose meaningful names such as studentName and totalMarks.
  • Remember that const does not make an object or array immutable.

9. Practice Task

Student Information Program

Write a JavaScript program that performs the following tasks:

  • Declare a student name using const.
  • Declare marks using let.
  • Display the student name and marks.
  • Update the marks and display the new value.
  • Create an if block and declare a variable using let.
  • Observe which variables can be accessed outside the block.
Challenge: Create one example each for var, let, and const, then explain their differences.

Important Points

  • var is function-scoped or globally scoped.
  • let is block-scoped and supports reassignment.
  • const is block-scoped and does not allow reassignment.
  • A const declaration must be initialized.
  • A let or const declaration cannot be redeclared in the same scope.
  • Prefer const by default and use let when reassignment is required.

Summary

JavaScript provides var, let, and const to declare variables. The var keyword is function-scoped or globally scoped, while let and const are block-scoped. We can reassign variables declared with var and let, but a const binding cannot be reassigned. Understanding scope and reassignment helps us write predictable, readable JavaScript programs.