Trending: Crack FAANG with our new JavaScript Interview Mastery!Try Now

React Mastery Course

0%

JavaScript Mastery

0%

Deep dive into the JS Engine, Call Stack, Event Loop, Closures, Promises, and the DOM with live interactive animations.

0 / 22 Lessons Completed
Up Next

What is JavaScript?

Continue
Beginner

What is JavaScript?

Interactive Environment: What is JavaScript?

Browser
HTML
CSS
JS Engine (V8)
Execution
DOM Update
Browser: The user requests a webpage. The browser receives the initial HTML response from the server.

The Brain of the Web

JavaScript is the programming language that makes websites interactive. Without JS, websites are just static text and images. When you click a button, fetch live data, or see an animation, JavaScript is doing the work behind the scenes. It runs inside a JavaScript Engine (like Google's V8) which translates your code into machine-readable instructions.

Why This Exists

In 1995, the web was just static text (HTML) and simple styles (CSS). Netscape created JavaScript in 10 days to add interactivity, allowing developers to validate forms, animate elements, and handle user clicks without reloading the page.

How it works Internally

When you run JavaScript, the browser doesn't execute it directly. It passes the code to the JS Engine (like V8 in Chrome). The Engine parses the code, creates an Abstract Syntax Tree (AST), compiles it into bytecode, and then the Call Stack executes it line-by-line. This entire process happens in milliseconds.

Real World Example

Almost everything interactive on the web: Netflix's video player, Google Maps' draggable interface, Facebook's infinite scrolling feed, and Web WhatsApp's real-time messaging.

Common Mistakes

Thinking JavaScript and Java are the same. They are completely different languages with different purposes. JS was named 'JavaScript' purely as a marketing trick in the 90s to piggyback on Java's popularity.

Performance & Security

JavaScript runs on the client-side (in the user's browser). This means you should never put sensitive data like passwords or secret API keys in your JavaScript code, because anyone can open DevTools and read it.

Mini Project: JS Execution Pipeline

Watch how a browser takes your JavaScript code and processes it through the V8 engine until it finally produces output.

How JavaScript Runs

High-level Architecture

Browser
JS Engine (V8)
Execution
Output
The browser downloads the HTML and JavaScript file.
script.js
1console.log("Hello, World!");
2// alert("Welcome to JavaScript!");
Line Explanation
Line 1

Console Logging

The console object provides access to the browser's debugging console. The log() method prints the string 'Hello, World!' to the terminal. It is used for debugging.

Quick Check

Question 1 of 2

Where does JavaScript execute?

Assignment: Your First Script

Write a JavaScript code snippet that logs your exact name to the console using console.log(). For example, if your name is Alex, you should log 'Alex'.

Editor
main.js
1
2
Console Output
No output yet. Run your code to see the result.

Interview Questions

3 questions to test your knowledge

Summary

JavaScript is an interpreted language executed by engines like V8. It's single-threaded, dynamically typed, and powers the interactivity of the modern web.

Up Next

Learn how JavaScript stores and manages data in memory.

Beginner

Variables

Interactive Environment: Variables

Variable Declaration

var vs let vs const in Memory

Global
var score
100
Block
let lives
3
Block
const MAX_LEVEL
50

Memory Boxes

Variables are named containers used to store data values in memory. Before 2015, JavaScript only had `var`. Modern JS introduced `let` and `const` to provide better scoping and prevent accidental data overrides.

Why This Exists

Programs need to remember data. If a user logs in, the program must store their 'username' in a variable to display it on the screen. Variables give names to memory addresses so developers don't have to write raw hex codes.

How it works Internally

When you declare `let score = 100`, JS allocates a slot in memory, assigns it the label 'score', and writes the value 100 into it. If you use `const`, the memory address becomes immutable (read-only for primitives).

Real World Example

In a Shopping Cart app, variables store the `totalPrice`, the `cartItems` array, and `isLoggedIn` boolean status.

Common Mistakes

Trying to reassign a `const` variable. Also, using `var` instead of `let`, which causes unpredictable scoping issues.

Performance & Security

Always prefer `const` by default. It signals to other developers (and the JS engine) that the variable won't change, which allows the engine to optimize memory access.

Mini Project: Variable Memory Visualizer

Watch how different variable declarations (var, let, const) behave when their values are updated.

Variable Declaration

var vs let vs const in Memory

Global
var score
100
Block
let lives
3
Block
const MAX_LEVEL
50
script.js
1let playerName = "Alex";
2let score = 0;
3const MAX_SCORE = 100;
4
5score = score + 10;
Line Explanation
Line 1

let Declaration

Declares a block-scoped local variable named 'playerName', initializing it to the string 'Alex'.

Predict the Output

Question 1 of 1
const age = 20;
age = 21;
console.log(age);

What will this code output?

Assignment: User Profile Variables

Declare a constant variable named 'id' set to 1. Declare a mutable variable 'score' using let, set to 0. Update 'score' to 10. Then console.log both variables.

Editor
main.js
1
2
Console Output
No output yet. Run your code to see the result.

Interview Questions

2 questions to test your knowledge

Summary

Use 'const' for values that never change. Use 'let' for values that change over time. Never use 'var'.

Beginner

Data Types

Interactive Environment: Data Types

JavaScript Data Types

Primitives (Value) vs Objects (Reference)

String"Hello"
Number42
Booleantrue
Undefinedundefined
Nullnull
SymbolSymbol('id')
BigInt9007199254740991n

Values vs Objects

JavaScript has two main categories of data types: Primitives (String, Number, Boolean, Undefined, Null, Symbol, BigInt) and References (Objects, Arrays, Functions).

Why This Exists

The computer needs to know how much memory to allocate and what operations are valid. You can multiply two Numbers, but multiplying two Strings doesn't make sense.

How it works Internally

Primitives are stored directly on the Stack because their size is fixed. Reference types are stored on the Heap because their size can grow dynamically, and the Stack only holds a pointer (memory address) to that Heap location.

Real World Example

A database row representing a User has Strings (name), Numbers (age), Booleans (isActive), and Objects (address).

Common Mistakes

Thinking `null` and `undefined` are the same. `undefined` means a variable was declared but not assigned a value. `null` is an intentional absence of any object value.

Performance & Security

Mutating reference types (Objects/Arrays) can cause severe bugs in complex apps (like React). Always create copies of objects rather than mutating them directly.

Mini Project: Memory Allocation Visualizer

Watch how Primitives are stored as values, while Objects are stored as references pointing to the heap.

JavaScript Data Types

Primitives (Value) vs Objects (Reference)

String"Hello"
Number42
Booleantrue
Undefinedundefined
Nullnull
SymbolSymbol('id')
BigInt9007199254740991n
script.js
1let str = "Hello";
2let num = 42;
3let obj1 = { id: 1 };
4let obj2 = obj1;
Line Explanation
Line 1

String Primitive

Allocates memory for a string primitive.

Predict the Output

Question 1 of 2
console.log(typeof null);

What does this output?

Assignment: Data Type Detective

Declare a String variable named `str`, a Number variable named `num`, and a Boolean variable named `bool`. Then log the `typeof` each variable to the console.

Editor
main.js
1
2
Console Output
No output yet. Run your code to see the result.

Interview Questions

3 questions to test your knowledge

Summary

Primitives are passed by value. Objects are passed by reference.

Beginner

Functions

Interactive Environment: Functions

Functions & The Call Stack

Execution Context & Return Values

script.js
function add(a, b) {
return a + b;
}

const result = add(5, 3);
Call Stack
Global Context
result:undefined

The Workhorses of JS

A function is a reusable block of code designed to perform a particular task. You define it once, and you can call (execute) it as many times as you want.

Why This Exists

Without functions, we would have to copy and paste the same lines of code every time we wanted to do a task, leading to massive, unmaintainable files.

How it works Internally

When you call a function, the JS Engine pushes a new 'Execution Context' onto the Call Stack. This context creates local memory for the function's parameters and variables. When the function returns, the context is popped off the stack and destroyed.

Real World Example

A shopping cart has a `calculateTax(price)` function. Instead of writing the math formula every time a user buys an item, you just call the function.

Common Mistakes

Forgetting to write the `return` keyword. If a function doesn't explicitly return a value, it implicitly returns `undefined`.

Performance & Security

Keep functions small and focused on a single task (Single Responsibility Principle). Massive functions with hundreds of lines are hard to test and debug.

Mini Project: Call Stack Visualizer

Watch how a function call creates a new execution context, binds arguments, and returns a value.

Functions & The Call Stack

Execution Context & Return Values

script.js
function add(a, b) {
return a + b;
}

const result = add(5, 3);
Call Stack
Global Context
result:undefined
script.js
1function add(a, b) {
2 return a + b;
3}
4
5let sum = add(5, 3);
Line Explanation
Line 1

Function Declaration

Defines a function named 'add' that accepts two parameters: a and b.

Predict the Output

Question 1 of 1
function doMath(x) {
  x * 2;
}
console.log(doMath(5));

What will this output?

Assignment: Tax Calculator

Write a function named `calculateTax` that takes a `price` as a parameter and returns the price multiplied by 1.2 (to add 20% tax). Then console.log the result of calculateTax(100).

Editor
main.js
1
2
3
4
5
Console Output
No output yet. Run your code to see the result.

Interview Questions

2 questions to test your knowledge

Summary

Functions take inputs (parameters), perform logic, and produce outputs (return values). They run in their own isolated execution contexts.

Beginner

Scope

Interactive Environment: Scope

Lexical Scope

Where can you access variables?

Global Scope (Window/Node)
const globalVar = 10;
Accessible everywhere
Function Scope
let funcVar = 20;
Hidden from Global
Block Scope (if, for)
const blockVar = 30;
Hidden from global

The Visibility Map

Scope determines the accessibility (visibility) of variables. JavaScript has three types of scope: Global Scope, Function Scope, and Block Scope.

Why This Exists

Scope prevents naming collisions. If every variable was global, two different scripts might both use a variable named `data` and overwrite each other, causing catastrophic bugs. Scope sandboxes variables.

How it works Internally

Scope is determined lexically at compile time. The JS engine maintains a Scope Chain. When you access a variable, it looks in the current scope. If it's not there, it looks in the outer parent scope, going up until it reaches the Global scope.

Real World Example

Like a hotel. Global scope is the lobby (everyone can see it). Function scope is your hotel room (only you can see inside). Block scope is the safe inside your hotel room.

Common Mistakes

Using `var` inside an `if` block or `for` loop. `var` does NOT have block scope, so it leaks out into the surrounding function or global scope.

Performance & Security

Limit the use of global variables. They pollute the global namespace and make it extremely difficult to track down state-related bugs.

Mini Project: Lexical Scope Visualizer

See how variables are sandboxed inside their respective scopes, and how inner scopes can reach outward, but outer scopes cannot reach inward.

Lexical Scope

Where can you access variables?

Global Scope (Window/Node)
const globalVar = 10;
Accessible everywhere
Function Scope
let funcVar = 20;
Hidden from Global
Block Scope (if, for)
const blockVar = 30;
Hidden from global
script.js
1let x = 10;
2if (true) {
3 let y = 20;
4 console.log(x);
5}
6// console.log(y);
Line Explanation
Line 1

Global Variable

'x' is declared in the global scope.

Spot the Bug

Question 1 of 1
for (let i = 0; i < 5; i++) {
  // looping
}
console.log(i);

Why does this throw an error?

Assignment: Scope Sandbox

Create a global variable named `globalName`. Then create a function `testScope` that defines a local variable `localName`. Console.log `globalName` inside the function, and execute the function.

Editor
main.js
1
2
Console Output
No output yet. Run your code to see the result.

Interview Questions

2 questions to test your knowledge

Summary

Inner scopes can reach out to access outer variables, but outer scopes cannot reach in. 'let' and 'const' respect block scopes (like loops and ifs).

Beginner

Hoisting

Interactive Environment: Hoisting

Hoisting Lifecycle

Creation vs Execution Phase

Code Load
Creation Phase
Execution Phase
Initialization
Execution Context
console.log(x); // Output?
var x = 5;
Console Output
Memory Environment
Scanning...
JS Engine scans the script

The Phantom Declarations

Hoisting is JavaScript's default behavior of moving variable and function declarations to the top of their respective scopes during the compilation phase, before the code is executed.

Why This Exists

Hoisting allows you to call functions before they appear in the code, which makes it easier to organize code in a top-down, readable manner without worrying about strict declaration order.

How it works Internally

During the 'Memory Creation' phase (before code runs), the engine scans for `var` and `function` keywords. It allocates memory for `var` and sets it to `undefined`. It allocates memory for `function` and stores the entire function body. `let` and `const` are also hoisted, but they are placed in a 'Temporal Dead Zone' and cannot be accessed.

Real World Example

In large files, developers usually put their main execution logic at the top of the file, and define all their helper functions at the bottom. Hoisting makes this possible.

Common Mistakes

Assuming Hoisting actually physically moves your code. It doesn't move anything; it just allocates memory for declarations before executing the code line-by-line.

Performance & Security

Relying on variable hoisting (`var`) makes code unreadable and prone to bugs. Use `let` and `const` to enforce strict Temporal Dead Zone rules, which forces you to declare variables before using them.

Mini Project: Hoisting Lifecycle Visualizer

Watch the difference between the Creation Phase (where memory is allocated) and the Execution Phase (where code runs).

Hoisting Lifecycle

Creation vs Execution Phase

Code Load
Creation Phase
Execution Phase
Initialization
Execution Context
console.log(x); // Output?
var x = 5;
Console Output
Memory Environment
Scanning...
JS Engine scans the script
script.js
1console.log(name);
2var name = "Alex";
3
4// console.log(age);
5// let age = 25;
Line Explanation
Line 1

Hoisted Var Access

Because 'var name' is hoisted and initialized with 'undefined' during memory creation, this logs 'undefined' instead of throwing a Reference Error.

Predict the Output

Question 1 of 1
sayHi();
var sayHi = function() {
  console.log("Hi");
}

What is the output?

Assignment: Hoisting Fix

The provided code tries to access a let variable before it's declared, causing a Temporal Dead Zone crash. Fix the order of the code so it prints successfully.

Editor
main.js
1
2
Console Output
No output yet. Run your code to see the result.

Interview Questions

2 questions to test your knowledge

Summary

Hoisting is the engine allocating memory before execution. 'var' initializes as undefined. 'let/const' stay uninitialized in the TDZ. Functions are fully loaded.

Intermediate

Arrays

Interactive Environment: Arrays

Interactive Array

Memory blocks shift dynamically as you modify the array.

Array Initialized
Index 0
apple
Index 1
banana
Index 2
cherry

The List Data Structure

Arrays are special objects used to store multiple values in a single variable. They use numbered indexes to access their elements, starting from 0.

Why This Exists

Without arrays, you would need to create a separate variable for every single item you wanted to store (e.g. user1, user2, user3). Arrays let you group them logically.

How it works Internally

In memory, traditional arrays are stored in contiguous (side-by-side) blocks. However, in JavaScript, arrays are technically hash maps (objects with integer keys), so they don't strictly require contiguous memory, though engines like V8 optimize them to act like it under the hood.

Real World Example

An Instagram feed is just a massive Array of post objects. A shopping cart is an Array of item objects.

Common Mistakes

Forgetting that array indexes start at 0, not 1. Also, modifying an array while looping over it can cause skipped elements or infinite loops.

Performance & Security

Adding elements to the end of an array (push) is very fast O(1). Adding elements to the beginning (unshift) is slow O(n) because every other element must have its index shifted up by 1.

Mini Project: Array Memory Visualizer

Watch how elements are stored in memory and accessed via their index.

Interactive Array

Memory blocks shift dynamically as you modify the array.

Array Initialized
Index 0
apple
Index 1
banana
Index 2
cherry
script.js
1const cart = ["Shirt"];
2cart.push("Pants");
3cart[0] = "Jacket";
4const last = cart.pop();
Line Explanation
Line 1

Initialization

Creates an array containing one string at index 0.

Predict the Output

Question 1 of 2
const arr = [1, 2, 3];
arr[5] = 6;
console.log(arr.length);

What is the output?

Assignment: Grocery List

Create an array called `groceries` with 2 items (e.g., 'Milk', 'Eggs'). Use a built-in array method to push a 3rd item ('Bread') to the end. Finally, console.log the entire array.

Editor
main.js
1
2
Console Output
No output yet. Run your code to see the result.

Interview Questions

2 questions to test your knowledge

Summary

Arrays are ordered lists of data accessed via 0-based indexing.

Intermediate

map()

Interactive Environment: map()

Array.map()

Transforms every element and returns a new array

const newArr = [1, 2, 3].map(n => n * 2);
Original Array
1
2
3
Callback: n * 2
New Array (Mapped)

The Transformer

The map() method creates a brand new array populated with the results of calling a provided function on every element in the calling array.

Why This Exists

Before map(), we used 'for' loops to transform data, which required manually creating an empty array, writing loop logic, and pushing items. map() is a declarative, cleaner alternative.

How it works Internally

map() allocates memory for a new array of the same length as the original. It executes the callback for index 0, places the return value in the new array at index 0, and continues. It does NOT mutate the original array.

Real World Example

In React, map() is used constantly to transform an array of data (like a list of user objects) into an array of UI components (like user profile cards).

Common Mistakes

Using map() when you aren't returning anything. If you just want to loop and execute side-effects (like console.log), use forEach(). Using map() without returning creates an array of 'undefined's.

Performance & Security

map() is generally fast, but chaining multiple array methods (.map().filter().map()) loops over the data multiple times. For massive datasets, a single reduce() or standard 'for' loop is faster.

Mini Project: Map Visualizer

Watch how a callback function processes each element individually to build a new array.

Array.map()

Transforms every element and returns a new array

const newArr = [1, 2, 3].map(n => n * 2);
Original Array
1
2
3
Callback: n * 2
New Array (Mapped)
script.js
1const users = [{name: 'Alex'}, {name: 'Bob'}];
2const names = users.map(user => user.name);
Line Explanation
Line 1

Object Array

An array containing two objects.

Spot the Bug

Question 1 of 1
const nums = [1, 2];
const doubled = nums.map(n => {
  n * 2;
});

Why does this map return [undefined, undefined]?

Assignment: Uppercase transformer

Use map() to transform an array of lowercase strings into an array of uppercase strings. Then console.log the new array.

Editor
main.js
1
2
3
Console Output
No output yet. Run your code to see the result.

Interview Questions

2 questions to test your knowledge

Summary

map() creates a new array of the same length by transforming each element.

Intermediate

filter()

Interactive Environment: filter()

Array.filter()

Keeps elements that pass the condition (return true)

const evens = [1, 2, 3, 4, 5].filter(n => n % 2 === 0);
Original Array
1
2
3
4
5
Condition: n % 2 === 0
Filtered Array (Kept Elements)

The Bouncer

The filter() method creates a shallow copy of a portion of a given array, filtered down to just the elements from the given array that pass the test implemented by the provided function.

Why This Exists

Filtering data is a core concept in programming. filter() abstracts away the loop logic, making it trivial to find subsets of data.

How it works Internally

filter() loops through the array. The callback function must return a boolean (true/false). If true, the element is pushed to the new array. If false, it is ignored.

Real World Example

Clicking the 'In Stock Only' checkbox on an e-commerce site runs a filter() over the products array, keeping only those where product.inStock === true.

Common Mistakes

Mutating the elements inside the filter callback. Filter should ONLY evaluate a condition and return a boolean, it shouldn't change the data.

Performance & Security

filter() returns a shallow copy. If the array contains objects, the new array contains references to the original objects. Modifying an object in the filtered array modifies the original object.

Mini Project: Filter Visualizer

Watch how elements are evaluated against a condition and either kept or discarded.

Array.filter()

Keeps elements that pass the condition (return true)

const evens = [1, 2, 3, 4, 5].filter(n => n % 2 === 0);
Original Array
1
2
3
4
5
Condition: n % 2 === 0
Filtered Array (Kept Elements)
script.js
1const tasks = [
2 { title: "Code", done: true },
3 { title: "Eat", done: false }
4];
5const pending = tasks.filter(t => !t.done);
Line Explanation
Line 1

Array of Objects

An array of task objects with boolean flags.

Predict the Output

Question 1 of 1
const words = ["a", "b", "c"];
const result = words.filter(w => w === "z");
console.log(result.length);

What is the output?

Assignment: Spam Filter

Filter an array of strings to remove the word 'spam'. Log the clean array to the console.

Editor
main.js
1
2
3
Console Output
No output yet. Run your code to see the result.

Interview Questions

2 questions to test your knowledge

Summary

filter() creates a new, smaller array containing only the elements that passed a boolean test.

Intermediate

reduce()

Interactive Environment: reduce()

Array.reduce()

Boils an array down to a single value via an accumulator

const sum = [10, 20, 30].reduce((acc, curr) => acc + curr, 0);
Accumulator (acc)
0
Initial Value
Callback: acc + curr
Current Array Value
10
20
30

The Aggregator

The reduce() method executes a 'reducer' callback function on each element of the array, passing in the return value from the calculation on the preceding element. The final result is a single value.

Why This Exists

While map and filter output arrays, sometimes you need to calculate a single number (a sum), or build a single object from an array of data. Reduce handles this elegantly.

How it works Internally

Reduce maintains an 'accumulator' variable. In every iteration, the callback function receives the current accumulator and the current array element. The callback must return the NEW accumulator value for the next iteration.

Real World Example

Calculating the total price of all items in a shopping cart. Or taking an array of users and reducing them into an object grouped by age.

Common Mistakes

Forgetting to provide the initial value (the second argument to reduce). If omitted, reduce uses the first element of the array as the initial value, which can cause severe bugs if the array is empty or contains objects.

Performance & Security

Reduce is incredibly powerful but can become very hard to read. If a reduce function is getting too complex, consider extracting the callback into a named function or using a standard 'for' loop for readability.

Mini Project: Reduce Visualizer

Watch the accumulator variable grow as it loops through the array.

Array.reduce()

Boils an array down to a single value via an accumulator

const sum = [10, 20, 30].reduce((acc, curr) => acc + curr, 0);
Accumulator (acc)
0
Initial Value
Callback: acc + curr
Current Array Value
10
20
30
script.js
1const sum = [10, 20].reduce((acc, val) => {
2 return acc + val;
3}, 0);
Line Explanation
Line 1

Reduce Init

Starts the reduce function. The '0' at the end is the initial value of the accumulator (acc).

Spot the Bug

Question 1 of 1
const arr = [];
const max = arr.reduce((acc, val) => Math.max(acc, val));

Why does this throw a TypeError on an empty array?

Assignment: Shopping Cart Total

Use reduce to calculate the total price of the items in the cart array. Then console.log the final total.

Editor
main.js
1
2
3
Console Output
No output yet. Run your code to see the result.

Interview Questions

2 questions to test your knowledge

Summary

reduce() takes an array and an initial value, and folds them into a single final value.

Intermediate

Objects

Interactive Environment: Objects

Objects & References

Heap Memory Allocation & Pass-by-Reference

const user = {};
Stack (Variables)
user
ref: 0x8F4A
Heap Memory
Object
0x8F4A
Empty Object

The Core of JS

Objects are collections of properties. A property is an association between a name (or key) and a value. A property's value can be a function, in which case the property is known as a method.

Why This Exists

Real world entities have multiple attributes. A 'Car' has a color, a brand, and a top speed. Storing these in separate variables is messy. Objects group related data together.

How it works Internally

In memory, an object is a reference type stored on the Heap. The keys are hashed to determine where in memory the corresponding value is stored, allowing for extremely fast O(1) lookups.

Real World Example

Almost all data sent over the internet (APIs) is formatted as JSON (JavaScript Object Notation).

Common Mistakes

Using arrow functions for object methods. Arrow functions do not bind their own 'this', so 'this.name' inside an arrow function method will be undefined.

Performance & Security

Be careful when iterating over objects with 'for...in' loops, as it will also iterate over inherited prototype properties. Use Object.keys() for safer iteration.

Mini Project: Object Memory Visualizer

See how properties and methods are bound to a central object reference in the heap.

Objects & References

Heap Memory Allocation & Pass-by-Reference

const user = {};
Stack (Variables)
user
ref: 0x8F4A
Heap Memory
Object
0x8F4A
Empty Object
script.js
1const car = {};
2car.brand = "Toyota";
3car["year"] = 2022;
4delete car.year;
Line Explanation
Line 1

Initialization

Creates an empty object literal.

Predict the Output

Question 1 of 1
const a = { x: 1 };
const b = { x: 1 };
console.log(a === b);

What is the output?

Assignment: Profile Builder

Create an object called `profile`. Give it a `username` property (string), a `followers` property (number initially 0), and a method `addFollower()` that increases `followers` by 1. Call `addFollower()` once and console.log the `profile` object.

Editor
main.js
1
2
3
4
Console Output
No output yet. Run your code to see the result.

Interview Questions

2 questions to test your knowledge

Summary

Objects group related data (properties) and behavior (methods) together.

Intermediate

Prototype

Interactive Environment: Prototype

Prototype Chain

How Inheritance works in JS

let arr = [1, 2];
let arr = [1, 2]
0: 1
1: 2

The Inheritance Chain

Prototypes are the mechanism by which JavaScript objects inherit features from one another. Instead of creating a copy of a method for every object, objects share a single method attached to a Prototype.

Why This Exists

Memory efficiency. If you create 1,000 array instances, it would crash the browser if every array had its own unique copy of the map() and filter() functions. Instead, they all just point to Array.prototype.

How it works Internally

Every object has a hidden internal property called `[[Prototype]]`. When you access a property, the engine looks at the object. If it fails, it follows the `[[Prototype]]` link to the parent object, continuing up the chain until it hits `null`.

Real World Example

When you use a library like jQuery or React, the components you build inherit core functionality from the library's base Prototype classes.

Common Mistakes

Modifying native prototypes (like Array.prototype.myNewMethod = ...). This is called 'Monkey Patching' and it is extremely dangerous because it can break third-party libraries that rely on standard behavior.

Performance & Security

Deep prototype chains can slow down property access time. If the engine has to jump through 5 prototypes to find a method, it is slower than finding it directly on the object.

Mini Project: Prototype Chain Explorer

Traverse the prototype chain from an array instance all the way up to Object.prototype.

Prototype Chain

How Inheritance works in JS

let arr = [1, 2];
let arr = [1, 2]
0: 1
1: 2
script.js
1const arr = [1, 2];
2console.log(arr.toString());
3console.log(arr.__proto__ === Array.prototype);
Line Explanation
Line 1

Initialization

Creates an array instance.

Predict the Output

Question 1 of 1
const obj = {};
console.log(obj.toString === Object.prototype.toString);

What is the output?

Assignment: Manual Inheritance

You have a `vehicle` object. Create a `car` object with a `brand` property. Use `Object.setPrototypeOf(child, parent)` to make `car` inherit from `vehicle`. Finally, console.log `car.wheels`.

Editor
main.js
1
2
3
4
Console Output
No output yet. Run your code to see the result.

Interview Questions

2 questions to test your knowledge

Summary

Objects inherit properties and methods from other objects via the Prototype Chain.

Intermediate

this Keyword

Interactive Environment: this Keyword

The 'this' Keyword

Context matters

Code Example
console.log(this);
Output
Window { ... } (Browser) Global { ... } (Node.js)
Why?

In the global execution context, 'this' refers to the global object.

The Chameleon Keyword

The `this` keyword refers to the object that is currently executing the code. Its value depends entirely on HOW the function is called, not where it was written.

Why This Exists

It allows a single function to be reused across multiple different objects, dynamically operating on whoever called it.

How it works Internally

During the Creation Phase of an Execution Context, the engine determines the 'thisBinding'. If called as a method (obj.func()), it binds to `obj`. If called normally (func()), it binds to the Global Object. If called with `new`, it binds to the newly constructed object.

Real World Example

In Object-Oriented UI components, `this` is used inside a class method to update that specific component's state (e.g. `this.setState()`).

Common Mistakes

Losing 'this' context when passing a method as a callback to a timer (setTimeout) or event listener. The browser calls the callback, changing 'this' to the Window object.

Performance & Security

If you need a function to ALWAYS have the same 'this', you can permanently bind it using `.bind()`, e.g., `const boundFunc = obj.method.bind(obj);`

Mini Project: Context Visualizer

Watch how the value of 'this' changes drastically depending on how a function is invoked.

The 'this' Keyword

Context matters

Code Example
console.log(this);
Output
Window { ... } (Browser) Global { ... } (Node.js)
Why?

In the global execution context, 'this' refers to the global object.

script.js
1const obj = {
2 id: 42,
3 logId: function() { console.log(this.id); },
4 arrowLog: () => { console.log(this.id); }
5};
Line Explanation
Line 1

Object Creation

Creating an object with an ID and two methods.

Predict the Output

Question 1 of 1
function showThis() {
  console.log(this === window);
}
showThis();

What is the output?

Assignment: Fix the Context

The code below defines a `delayedGreet` method. But if you run it, it logs `undefined` because `setTimeout`'s callback is a regular function. Fix it by converting the callback inside setTimeout to an Arrow Function, which will inherit `this` from `delayedGreet`.

Editor
main.js
1
2
3
4
5
6
7
8
9
Console Output
No output yet. Run your code to see the result.

Interview Questions

2 questions to test your knowledge

Summary

'this' refers to the object executing the function. Arrow functions inherit 'this' lexically.

Advanced

DOM

Interactive Environment: DOM

Document Object Model

HTML → DOM → JS → UI Update

index.html
<body>
  <h1 id="title">Hello</h1>
</body>
script.js
const title = document.getElementById('title');
title.innerText = "Changed by JS!";
DOM Tree (Memory)
Waiting for HTML...
1. Browser downloads HTML

The Bridge to UI

The Document Object Model (DOM) is an object-oriented representation of the web page. It allows JavaScript to read, add, change, or delete HTML elements dynamically.

Why This Exists

Without the DOM API, JavaScript would be trapped inside its engine, unable to see or modify the actual website the user is looking at.

How it works Internally

When the browser downloads HTML, it parses it into a tree structure in memory (the DOM Tree). Every HTML tag becomes an Object (a Node). JS interacts with these Node objects, not the raw text.

Real World Example

When you switch a website to 'Dark Mode', JavaScript is using the DOM to find the <body> tag and append a 'dark' CSS class to it.

Common Mistakes

Trying to access a DOM element before the HTML has finished loading. If your JS runs in the <head> before the <body> exists, document.getElementById() will return null.

Performance & Security

DOM manipulation is SLOW. Modifying the DOM triggers a browser 'reflow' and 'repaint', which is computationally expensive. This is why React invented the Virtual DOM to minimize direct DOM updates.

Mini Project: DOM Tree Visualizer

Watch how HTML text is parsed into a tree of objects, which JavaScript then modifies.

Document Object Model

HTML → DOM → JS → UI Update

index.html
<body>
  <h1 id="title">Hello</h1>
</body>
script.js
const title = document.getElementById('title');
title.innerText = "Changed by JS!";
DOM Tree (Memory)
Waiting for HTML...
1. Browser downloads HTML
script.js
1const btn = document.createElement('button');
2btn.innerText = 'Click Me';
3document.body.appendChild(btn);
Line Explanation
Line 1

Element Creation

Tells the DOM to create a brand new <button> node in memory. It is not on the screen yet.

Predict the Output

Question 1 of 1
// Which one?

How do you select all elements with the class 'card'?

Assignment: Memory DOM

Use `document.createElement('h1')` to create a heading in memory. Set its `innerText` to 'Hello DOM!'. Then `console.log(el.outerHTML)` to see the resulting HTML string.

Editor
main.js
1
2
Console Output
No output yet. Run your code to see the result.

Interview Questions

2 questions to test your knowledge

Summary

The DOM is a tree of objects representing HTML. JS uses the DOM API to modify the UI.

Advanced

Events

Interactive Environment: Events

Event Listeners

Reacting to user actions

Event
Triggered
Listener
Catches
DOM
Updated
script.js
btn.addEventListener('click', () => {
// Handler Executes
btn.innerText = "Subscribed!";
btn.classList.add("bg-green");
});

Action & Reaction

Events are actions that happen in the browser (clicks, typing, scrolling). JavaScript can 'listen' for these events and execute a function (a Handler) when they occur.

Why This Exists

If we couldn't detect user actions, the web would be a read-only medium like a newspaper. Events make the web interactive.

How it works Internally

When an event occurs, the browser creates an Event Object containing details (like X/Y coordinates). The event trickles down the DOM (Capturing), hits the target, and then bubbles back up (Bubbling).

Real World Example

Clicking 'Submit' on a login form triggers a 'submit' event. Typing in a search bar triggers a 'keyup' event for live search results.

Common Mistakes

Forgetting to remove event listeners when elements are deleted. In single-page apps (like React), this leads to severe memory leaks as the hidden listeners pile up.

Performance & Security

Attaching an event listener to 1,000 individual list items will crash the browser. Instead, attach ONE listener to the parent <ul> and let the events bubble up to it (Event Delegation).

Mini Project: Event Pipeline Visualizer

Watch the lifecycle of a click event from trigger to listener to DOM update.

Event Listeners

Reacting to user actions

Event
Triggered
Listener
Catches
DOM
Updated
script.js
btn.addEventListener('click', () => {
// Handler Executes
btn.innerText = "Subscribed!";
btn.classList.add("bg-green");
});
script.js
1document.body.addEventListener('keypress', (e) => {
2 if (e.key === 'Enter') {
3 submitForm();
4 }
5});
Line Explanation
Line 1

Adding Listener

Attaches an event listener to the entire body, listening for any key press.

Quick Check

Question 1 of 1

What phase comes first in the DOM event flow?

Assignment: Manual Event Trigger

Create an element, add a click listener that logs 'Clicked!', and then manually fire a click event using `.click()`.

Editor
main.js
1
2
3
4
Console Output
No output yet. Run your code to see the result.

Interview Questions

2 questions to test your knowledge

Summary

Events allow JS to react to user actions. You use addEventListener to attach handler functions to DOM elements.

Advanced

Promises

Interactive Environment: Promises

Promises

The 3 States of Async Operations

Promise Object
Pending...
Waiting for data
.then(callback)
Waiting for Fulfillment...
.catch(callback)
Waiting for Rejection...

The IOU of JavaScript

A Promise is an object that represents the eventual completion (or failure) of an asynchronous operation and its resulting value. It is an 'IOU' for data that isn't ready yet.

Why This Exists

Before Promises, JS relied heavily on Callbacks to handle async tasks (like fetching data). This led to 'Callback Hell'—deeply nested, unreadable code. Promises flattened this into neat `.then()` chains.

How it works Internally

A Promise has 3 states: Pending, Fulfilled (Resolved), and Rejected. When a Promise resolves, it pushes its `.then()` callbacks to the Microtask Queue in the Event Loop, which has priority over standard callbacks.

Real World Example

When you upload a video to YouTube, it doesn't process instantly. The upload function returns a Promise. When the server finishes processing, the Promise 'resolves' and updates your dashboard.

Common Mistakes

Forgetting to return a Promise inside a `.then()` chain. If you don't return it, the next `.then()` will execute immediately with `undefined` instead of waiting.

Performance & Security

Always, ALWAYS include a `.catch()` at the end of a Promise chain. Unhandled Promise rejections can crash Node.js applications.

Mini Project: Promise States Visualizer

Watch a Promise object transition from pending to resolved/rejected, and trigger the appropriate callback.

Promises

The 3 States of Async Operations

Promise Object
Pending...
Waiting for data
.then(callback)
Waiting for Fulfillment...
.catch(callback)
Waiting for Rejection...
script.js
1fetchData()
2 .then(user => fetchPosts(user.id))
3 .then(posts => console.log(posts))
4 .catch(err => alert(err));
Line Explanation
Line 1

Async Call

fetchData() returns a Promise. We don't have the user yet, but we have the Promise of a user.

Predict the Output

Question 1 of 1
console.log(1);
Promise.resolve().then(() => console.log(2));
console.log(3);

What is the output order?

Assignment: Promise Chain

Create a resolved Promise using `Promise.resolve('Data Ready!')`. Chain a `.then()` to it and console.log the result.

Editor
main.js
1
2
Console Output
No output yet. Run your code to see the result.

Interview Questions

1 questions to test your knowledge

Summary

Promises represent future values. They start Pending, and end up either Resolved (success) or Rejected (failure).

Advanced

Async Await

Interactive Environment: Async Await

Async / Await

Pausing execution without blocking

JS Thread (Call Stack)
async function getUser() {
// 1. Synchronous
console.log("Fetching...");
// 2. Await (Pause function!)
const res = await fetch('/api');
// 3. Resumes when data arrives
console.log("Done!", res);
}
Remote Server

The Pause Button

Async/Await is syntactic sugar built on top of Promises. It allows you to write asynchronous code that reads exactly like synchronous code, making it infinitely easier to read and debug.

Why This Exists

While Promises fixed Callback Hell, long `.then()` chains were still hard to read, especially when dealing with complex if/else logic or sharing variables between different `.then()` blocks.

How it works Internally

When the JS Engine hits the `await` keyword, it literally pauses the execution of that specific `async` function and yields control back to the Event Loop, allowing other code to run. When the Promise resolves, the function is pushed back onto the stack and resumes right where it left off.

Real World Example

Almost all modern JavaScript codebases use Async/Await for database queries, API calls, and file system reads.

Common Mistakes

Using `await` inside a standard loop like `forEach()`. `forEach` is not async-aware and will fire all iterations instantly without waiting. Use a `for...of` loop instead if you need to await inside a loop.

Performance & Security

Awaiting Promises sequentially when they don't depend on each other. If API 2 doesn't need API 1's data, use `await Promise.all([fetch1, fetch2])` to execute them concurrently, halving the load time.

Mini Project: Execution Pauser Visualizer

Watch how the 'await' keyword pauses function execution, yields to the server, and resumes when data arrives.

Async / Await

Pausing execution without blocking

JS Thread (Call Stack)
async function getUser() {
// 1. Synchronous
console.log("Fetching...");
// 2. Await (Pause function!)
const res = await fetch('/api');
// 3. Resumes when data arrives
console.log("Done!", res);
}
Remote Server
script.js
1async function getData() {
2 const p1 = await fetch('/api/1');
3 const p2 = await fetch('/api/2');
4 return [p1, p2];
5}
Line Explanation
Line 1

Async Keyword

The 'async' keyword tells the engine this function will use 'await'. Async functions ALWAYS return a Promise implicitly.

Spot the Bug

Question 1 of 1
async function getNum() {
  return 5;
}
const x = getNum();

Why does this function return a Promise, not the data?

Assignment: Async Refactor

Write an `async function run()` that uses `await` to pause on `Promise.resolve('Success!')`, stores the result in a variable, and logs it. Call `run()` at the end.

Editor
main.js
1
2
Console Output
No output yet. Run your code to see the result.

Interview Questions

1 questions to test your knowledge

Summary

Async/Await lets you pause execution while waiting for Promises to resolve, making async code read top-to-bottom.

Advanced

Fetch API

Interactive Environment: Fetch API

Fetch API

Client-Server Communication

Client Browser
User Interface
API Server
Idle

The Network Courier

The Fetch API provides a modern, Promise-based interface for fetching resources (including across the network). It replaces the older, clunky XMLHttpRequest.

Why This Exists

Web applications need to get data from databases and servers without reloading the entire web page. Fetch allows silent background network requests.

How it works Internally

Fetch delegates the actual HTTP request to the browser's Network Thread (written in C++). JS doesn't do the networking itself. When the Network Thread gets the response headers, the Fetch promise resolves with a Response object. You then call `.json()` (which returns a second Promise) to read the body stream.

Real World Example

When you open Twitter, the initial HTML shell loads instantly. Then, Fetch API requests are made to Twitter's servers to get your timeline JSON data, which is then rendered on screen.

Common Mistakes

Assuming a Fetch promise rejects on HTTP errors (like 404 Not Found or 500 Server Error). It DOES NOT. It only rejects on network failures (like losing internet connection). You must manually check `response.ok`.

Performance & Security

Never send sensitive data (passwords, tokens) over a standard HTTP connection using Fetch; it will be sent in plain text. Always ensure the API uses HTTPS.

Mini Project: Network Request Visualizer

Watch a Fetch request travel from the client, get processed by the API server, and return a JSON response.

Fetch API

Client-Server Communication

Client Browser
User Interface
API Server
Idle
script.js
1const res = await fetch('/api/login', {
2 method: 'POST',
3 headers: { 'Content-Type': 'application/json' },
4 body: JSON.stringify({ email: 'test@test.com' })
5});
Line Explanation
Line 1

Fetch Call

Initiates a network request. The first argument is the URL. The second is an options object.

Quick Check

Question 1 of 1

Which HTTP method should you use to update an existing resource?

Assignment: API Caller

Write code (using .then or async/await) that fetches from `https://jsonplaceholder.typicode.com/users/1`, parses the JSON via `.json()`, and console.logs the user's `name`.

Editor
main.js
1
2
3
Console Output
No output yet. Run your code to see the result.

Interview Questions

2 questions to test your knowledge

Summary

Fetch is the modern, Promise-based way to make HTTP requests and interact with APIs.

Module 4

The Event Loop

Interactive Environment: The Event Loop

The Event Loop

Call Stack, Web APIs, Callback Queue, and the Event Loop

script.js
console.log("Start");
setTimeout(() => {
  console.log("Timeout");
}, 2000);
console.log("End");
Console Output
_
Call Stack
Empty
Web APIs
Event Loop checks Stack
Callback Queue
Empty

The Async Conductor

JavaScript is single-threaded, meaning it can only execute one task at a time. The Event Loop is the secret mechanism that allows it to perform non-blocking operations like fetching data or setting timers without freezing the entire page.

Why This Exists

If JavaScript blocked the main thread every time it waited for a network request, websites would completely freeze. The Event Loop solves this by offloading slow tasks to the browser and executing them when ready.

How it works Internally

When a function runs, it enters the Call Stack. Async operations (like setTimeout) are moved to Web APIs. When they finish, their callback is placed in the Callback Queue. The Event Loop constantly checks if the Call Stack is empty; if it is, it pushes the first task from the Queue into the Stack.

Real World Example

Every time you scroll a feed while a video loads in the background, or click a button while a form submits, the Event Loop is orchestrating these multiple events simultaneously.

Common Mistakes

Many developers think `setTimeout(() => console.log('A'), 0)` will execute 'A' immediately because of the 0ms delay. However, setTimeout ALWAYS goes to the Web API and Queue, meaning synchronous code always finishes first, followed by 'A'.

Performance & Security

Long-running synchronous loops (like a massive `for` loop) will block the Call Stack, meaning the Event Loop cannot process UI updates or clicks. This leads to the infamous "Page Unresponsive" error. Break up heavy computation using Web Workers.

script.js
1console.log('1: Script start');
2
3setTimeout(() => {
4 console.log('4: setTimeout');
5}, 0);
6
7Promise.resolve().then(() => {
8 console.log('3: Promise microtask');
9});
10
11console.log('2: Script end');
Line Explanation
Line 1

Synchronous Execution

Synchronous code runs first (1, 2) directly on the Call Stack.

Predict the Output

Question 1 of 1
console.log("A");
setTimeout(() => console.log("B"), 0);
console.log("C");

What is the output order?

Assignment: Async Timers

Console.log the string 'Start'. Then use `setTimeout` to console.log 'Delayed' after a 0ms delay. Finally, console.log 'End'.

Editor
main.js
1
2
Console Output
No output yet. Run your code to see the result.

Interview Questions

2 questions to test your knowledge

Summary

The Event Loop is the conductor of asynchronous JavaScript, moving callbacks from the Queue to the Stack when the engine is idle.

Up Next

Modern JavaScript (ES6+)

Module 5

Destructuring & Spread

Interactive Environment: Destructuring & Spread

ES6 Features

Destructuring and Spread Operators

const user = { name: 'Alex', age: 25 }; const arr1 = [1, 2]; const arr2 = [3, 4];
user
name: "Alex"
age: 25
arr1
1
2
arr2
3
4

Syntactic Sugar

ES6 introduced Destructuring to unpack values from arrays or properties from objects into distinct variables easily. The Spread operator (`...`) allows an iterable (like an array or object) to be expanded in places where multiple arguments or elements are expected.

Why This Exists

Before ES6, extracting properties from objects required writing tedious `var name = user.name;` lines repeatedly. Copying arrays required `slice()` or `concat()`. ES6 syntax makes code incredibly concise and readable.

How it works Internally

Under the hood, destructuring still performs the same assignment operations, but it is syntactic sugar provided by the parser. The spread operator iterates through the object/array properties and shallow copies them into the new structure.

Real World Example

React developers use destructuring in almost every component (`const { title, image } = props;`). The spread operator is essential for immutably updating state (e.g., `setUsers([...users, newUser])`).

Common Mistakes

The spread operator only performs a SHALLOW copy. If you have nested objects inside, the clone still references the original nested object. Changing it mutates the original.

Performance & Security

While spread operators are heavily used, spreading massive arrays (millions of elements) can cause Call Stack Size Exceeded errors or performance hits. For huge datasets, traditional loops are sometimes more performant.

script.js
1// Destructuring
2const user = { id: 1, profile: { name: 'Alex' } };
3const { id, profile: { name } } = user;
4
5// Spread Operator
6const arr1 = [1, 2];
7const arr2 = [3, 4];
8const combined = [...arr1, ...arr2];
Line Explanation
Line 1

Destructuring

Destructuring can be nested (pulling `name` out of `profile`).

Predict the Output

Question 1 of 1
const obj1 = { a: 1, b: 2 };
const obj2 = { b: 3, c: 4 };
const combined = { ...obj1, ...obj2 };
console.log(combined.b);

What is the output?

Assignment: Spread & Destructure

You have an object `user`. Use destructuring to extract `username`. Then use the spread operator to merge `arr1` and `arr2` into a new array called `merged`. Console.log both `username` and `merged`.

Editor
main.js
1
2
3
4
5
6
Console Output
No output yet. Run your code to see the result.

Interview Questions

2 questions to test your knowledge

Summary

Destructuring and Spread are syntactic sugar that will halve the amount of code you write while doubling its readability.

Up Next

Classes & OOP

Module 5

Classes & OOP

Interactive Environment: Classes & OOP

Classes & Inheritance

Object-Oriented Programming in JS

class Animal { constructor(name) { this.name = name; } speak() { console.log('Noise'); } }
Parent Class
Animal
Constructorname
Methodspeak() = "Noise"

Syntactic Blueprints

ES6 Classes provide a much simpler and clearer syntax to create objects and deal with inheritance. However, they are primarily syntactic sugar over JavaScript's existing prototype-based inheritance.

Why This Exists

JavaScript's original prototype system (using Constructor functions and `prototype` chain modification) was confusing and repelled developers coming from Java or C++. The `class` keyword was introduced to provide a familiar OOP structure.

How it works Internally

When you define a `class`, JavaScript is still creating a constructor function behind the scenes. When you write a method inside a class, it is automatically added to the constructor's `.prototype` object.

Real World Example

While React has moved towards functional components, game development (HTML5 Canvas), backend systems (NestJS, TypeORM), and complex UI architectures still heavily rely on Object-Oriented patterns and classes.

Common Mistakes

Losing `this` context. When `btn.click` is passed as a callback, it loses the class context. You must bind it: `setTimeout(btn.click.bind(btn), 100)` or use an arrow function method.

Performance & Security

Classes use prototype-based inheritance, meaning methods are shared in memory across all instances. This makes them highly memory-efficient if you are instantiating thousands of objects (like enemies in a game).

script.js
1class Animal {
2 constructor(name) {
3 this.name = name;
4 }
5 speak() {
6 return `${this.name} makes a noise.`;
7 }
8}
9
10class Dog extends Animal {
11 speak() {
12 return `${this.name} barks!`;
13 }
14}
Line Explanation
Line 1

Inheritance

`extends` automatically sets up the prototype chain.

Predict the Output

Question 1 of 1
class Car {
  constructor() { this.wheels = 4; }
}
const myCar = Car();
console.log(myCar.wheels);

What is the output?

Assignment: Build a Class

Create a class named `Player`. Give it a `constructor` that takes a `name` and sets `this.name = name` and `this.score = 0`. Add a method `addScore()` that increments score by 1. Instantiate it as `p1`, call `addScore()`, and console.log `p1.score`.

Editor
main.js
1
2
Console Output
No output yet. Run your code to see the result.

Interview Questions

2 questions to test your knowledge

Summary

Classes provide a clean, readable syntax for creating objects and structuring complex, inheritance-heavy code.

Up Next

ES6 Modules

Module 5

ES6 Modules

Interactive Environment: ES6 Modules

ES6 Modules

Importing and Exporting between files

Before Modules: Files don't share variables by default.
math.js
Function
const add = (a, b) => a + b;
app.js
Import Statement
import { add } from './math.js';

File Architectures

Modules allow you to break your code into separate files. You can `export` functions, objects, or primitives from one file, and `import` them into another.

Why This Exists

Before modules, developers had to rely on a single massive `script.js` file, or include dozens of `<script>` tags in the HTML in a very specific order. Modules allow scalable, maintainable file architectures without polluting the global namespace.

How it works Internally

When you import a module, it is evaluated exactly once. Subsequent imports of the same module share the same instance in memory (making modules effectively singletons). The imports are strictly statically analyzed.

Real World Example

Every modern web framework (React, Vue, Angular) and Node.js environment uses the ES Module system to organize thousands of files into a cohesive application.

Common Mistakes

Mixing default and named imports. If you export default, you must import it without curly braces: `import add from './math.js'`.

Performance & Security

Modules automatically run in "strict mode". By separating code into modules, bundlers like Webpack or Vite can perform "Tree Shaking"—removing exported functions that are never actually imported, drastically reducing bundle size.

script.js
1// ----- utils.js -----
2export const multiply = (a, b) => a * b;
3export default function logger(msg) {
4 console.log(msg);
5}
6
7// ----- main.js -----
8import logger, { multiply } from './utils.js';
Line Explanation
Line 1

Named vs Default

You can combine a default import (`logger`) and a named import (`multiply`) in the same statement.

Quick Check

Question 1 of 1

How do you correctly import a named export 'Button' from a file?

Assignment: Template Literals

Because our sandbox doesn't support multiple files for `import`/`export`, let's practice another crucial ES6 feature: Template Literals. Create a variable `name = 'Alex'` and `age = 25`. Use backticks (`) to console.log the exact string: `Hi, I am Alex and I am 25 years old.`

Editor
main.js
1
2
3
4
5
Console Output
No output yet. Run your code to see the result.

Interview Questions

2 questions to test your knowledge

Summary

Modules are the backbone of modern JavaScript architecture, keeping your codebase organized and globally scoped variables at zero.