Skip to content

Commit 9a5f619

Browse files
committed
Add JavaScript Zero to Hero course content
9 teaching modules: basics (variables, types, operators), control flow, iteration, collections (arrays, objects, Map, Set), functions (closures, higher-order, callbacks), OOP (classes, inheritance, private fields), error handling, async (Promises, async/await), and DOM manipulation. Part of the Zero to Hero course family.
0 parents  commit 9a5f619

12 files changed

Lines changed: 1277 additions & 0 deletions

File tree

‎.gitignore‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
node_modules/
2+
.env
3+
*.log
4+
.DS_Store
5+
Thumbs.db
6+
.idea/
7+
.vscode/

‎README.md‎

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# JavaScript Zero to Hero
2+
3+
A structured teaching repository covering JavaScript from variables and types through to async/await, DOM manipulation, and ES6+ patterns. Each module is a standalone, runnable file with inline commentary. Part of the Zero to Hero course family.
4+
5+
---
6+
7+
## Who Is This For?
8+
9+
- Developers learning JavaScript for the first time
10+
- Delegates on instructor-led web development courses
11+
- Self-learners who want a clear, progressive path through the language
12+
13+
No prior JavaScript experience is assumed. Basic comfort with a command line is helpful.
14+
15+
---
16+
17+
## Prerequisites
18+
19+
| Tool | Minimum Version |
20+
|---------|----------------|
21+
| Node.js | 18+ |
22+
23+
```bash
24+
node --version
25+
```
26+
27+
Most modules run in Node.js. The `dom/` module is designed for browser use.
28+
29+
---
30+
31+
## Getting Started
32+
33+
```bash
34+
# Clone the repository
35+
git clone git@github.com:Edrzapi/JavaScript_zero_hero.git
36+
cd JavaScript_zero_hero
37+
38+
# Run any module directly
39+
node basics/01_variables_types.js
40+
node functions/01_functions.js
41+
node async/01_promises_async.js
42+
```
43+
44+
No dependencies to install — the core modules use only the standard language.
45+
46+
---
47+
48+
## Repository Structure
49+
50+
```
51+
├── basics/ # Variables, types, coercion, operators, destructuring
52+
├── control_flow/ # if/else, switch, ternary
53+
├── iteration/ # for, while, for...of, for...in, forEach
54+
├── collections/ # Arrays, objects, Map, Set
55+
├── functions/ # Declarations, arrows, closures, higher-order, callbacks
56+
├── oop/ # Classes, inheritance, private fields, static, polymorphism
57+
├── exceptions/ # try/catch/finally, custom error classes
58+
├── async/ # Callbacks, Promises, async/await, Promise.all
59+
├── dom/ # Selectors, events, dynamic content (browser)
60+
├── testing/ # Testing patterns (in development)
61+
├── apis/ # HTTP requests (in development)
62+
└── tasks/ # Hands-on exercises (in development)
63+
```
64+
65+
---
66+
67+
## Topics Covered
68+
69+
### Core Language
70+
71+
- **Variables and Types** — `let`/`const`/`var`, primitives, template literals, type coercion, `===` vs `==`, truthy/falsy.
72+
- **Operators** — Arithmetic, comparison, logical, nullish coalescing (`??`), optional chaining (`?.`), destructuring, spread.
73+
- **Control Flow** — `if`/`else`, `switch`, ternary expressions.
74+
- **Iteration** — `for`, `while`, `do...while`, `for...of`, `for...in`, `break`/`continue`, `forEach`.
75+
76+
### Data Structures
77+
78+
- **Arrays** — `push`/`pop`, `map`/`filter`/`reduce`, `find`/`some`/`every`, sorting, spread copy.
79+
- **Objects** — Dot/bracket notation, `Object.keys`/`values`/`entries`, spread merge.
80+
- **Map and Set** — Keyed collections, unique values, deduplication.
81+
82+
### Functions
83+
84+
- **Declarations and Arrows** — Hoisting, concise syntax, default parameters, rest parameters.
85+
- **Higher-Order Functions** — Functions as arguments and return values, factory pattern.
86+
- **Closures** — Encapsulation via captured scope, counter pattern.
87+
- **Callbacks** — Async foundation, event-driven patterns.
88+
89+
### Object-Oriented Programming
90+
91+
- **Classes** — Constructors, methods, `toString()`.
92+
- **Inheritance** — `extends`, `super`, method overriding, `instanceof`.
93+
- **Encapsulation** — Private fields (`#`), getters/setters.
94+
- **Polymorphism** — Shape hierarchy, dynamic dispatch.
95+
- **Static Members** — Class-level methods and properties.
96+
97+
### Error Handling
98+
99+
- **try/catch/finally** — Exception flow, re-throwing, `instanceof` checks.
100+
- **Custom Errors** — Extending `Error`, typed error handling.
101+
102+
### Async JavaScript
103+
104+
- **Callbacks** — setTimeout, callback pattern, callback hell.
105+
- **Promises** — `new Promise`, `.then`/`.catch`, chaining.
106+
- **async/await** — Syntactic sugar, error handling with try/catch.
107+
- **Promise.all** — Parallel execution, `allSettled`, `race`, `any`.
108+
109+
### DOM (Browser)
110+
111+
- **Selectors** — `querySelector`, `querySelectorAll`, `getElementById`.
112+
- **Manipulation** — `textContent`, `innerHTML`, `classList`, attributes.
113+
- **Events** — `addEventListener`, event delegation, form handling.
114+
- **Dynamic Content** — `createElement`, `appendChild`, `remove`.
115+
116+
---
117+
118+
## Teaching Approach
119+
120+
- Each file is standalone — run it directly with `node` or in a browser console
121+
- Concepts are explained in comments alongside working code
122+
- Topics progress from simple to complex within each module
123+
- No frameworks, no build tools — pure JavaScript throughout
124+
- DOM module is designed for browser use; all other modules run in Node.js
125+
126+
---
127+
128+
## Tech Stack
129+
130+
| Technology | Purpose |
131+
|---------------|---------|
132+
| JavaScript | Language (ES2022+) |
133+
| Node.js 18+ | Runtime for non-browser modules |
134+
135+
No React. No TypeScript. No bundlers. Pure JavaScript.
136+
137+
---
138+
139+
## What's Next?
140+
141+
This course covers JavaScript fundamentals. For React, Express, and applied web development, see the related repositories on this profile.
142+
143+
For the same structured approach in other languages, see the companion courses:
144+
145+
- [Python Zero to Hero](https://github.com/Edrzapi/Python_zero_hero)
146+
- [Java Zero to Hero](https://github.com/Edrzapi/Java_zero_hero)
147+
- [C# Zero to Hero](https://github.com/Edrzapi/CS_zero_hero)
148+
- [SQL Zero to Hero](https://github.com/Edrzapi/SQL_zero_hero)

‎async/01_promises_async.js‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// ============================================================
2+
// Async JavaScript — Callbacks, Promises, async/await
3+
// ============================================================
4+
5+
// ------------------------------------------------------------
6+
// Callbacks — the original async pattern
7+
// ------------------------------------------------------------
8+
9+
console.log("=== Callbacks ===");
10+
11+
function fetchUser(id, callback) {
12+
setTimeout(() => {
13+
callback({ id, name: "Alice" });
14+
}, 100);
15+
}
16+
17+
fetchUser(1, (user) => {
18+
console.log("Callback result:", user);
19+
});
20+
21+
// Problem: nested callbacks become unreadable ("callback hell")
22+
23+
// ------------------------------------------------------------
24+
// Promises — cleaner async handling
25+
// ------------------------------------------------------------
26+
27+
console.log("\n=== Promises ===");
28+
29+
function fetchUserPromise(id) {
30+
return new Promise((resolve, reject) => {
31+
setTimeout(() => {
32+
if (id > 0) {
33+
resolve({ id, name: "Bob" });
34+
} else {
35+
reject(new Error("Invalid ID"));
36+
}
37+
}, 100);
38+
});
39+
}
40+
41+
// .then() / .catch() chain
42+
fetchUserPromise(1)
43+
.then(user => console.log("Promise result:", user))
44+
.catch(err => console.log("Error:", err.message));
45+
46+
// Chaining — each .then() returns a new Promise
47+
fetchUserPromise(2)
48+
.then(user => user.name.toUpperCase())
49+
.then(name => console.log("Chained:", name))
50+
.catch(err => console.log("Error:", err.message));
51+
52+
// ------------------------------------------------------------
53+
// async / await — syntactic sugar over Promises
54+
// ------------------------------------------------------------
55+
56+
console.log("\n=== async/await ===");
57+
58+
async function getUser(id) {
59+
try {
60+
const user = await fetchUserPromise(id);
61+
console.log("Async result:", user);
62+
return user;
63+
} catch (error) {
64+
console.log("Async error:", error.message);
65+
}
66+
}
67+
68+
// Call async function
69+
getUser(1);
70+
71+
// Error handling with async/await
72+
async function getUserOrFail() {
73+
try {
74+
const user = await fetchUserPromise(-1); // will reject
75+
console.log(user);
76+
} catch (error) {
77+
console.log("Caught async error:", error.message);
78+
}
79+
}
80+
81+
getUserOrFail();
82+
83+
// ------------------------------------------------------------
84+
// Promise.all — run multiple promises in parallel
85+
// ------------------------------------------------------------
86+
87+
async function fetchAll() {
88+
const [user1, user2, user3] = await Promise.all([
89+
fetchUserPromise(1),
90+
fetchUserPromise(2),
91+
fetchUserPromise(3),
92+
]);
93+
console.log("\nPromise.all results:", user1, user2, user3);
94+
}
95+
96+
fetchAll();
97+
98+
// Promise.allSettled — like all, but doesn't short-circuit on failure
99+
// Promise.race — resolves/rejects with the first settled promise
100+
// Promise.any — resolves with the first fulfilled promise
101+
102+
// ------------------------------------------------------------
103+
// Fetch API (browser/Node 18+)
104+
// ------------------------------------------------------------
105+
106+
// async function fetchPokemon() {
107+
// const response = await fetch("https://pokeapi.co/api/v2/pokemon/pikachu");
108+
// if (!response.ok) throw new Error(`HTTP ${response.status}`);
109+
// const data = await response.json();
110+
// console.log(data.name, data.height, data.weight);
111+
// }
112+
113+
// Uncomment and run with Node 18+ or in a browser console.
114+
115+
// ------------------------------------------------------------
116+
// Key Rules
117+
// ------------------------------------------------------------
118+
119+
// 1. async functions always return a Promise
120+
// 2. await can only be used inside async functions (or top-level in modules)
121+
// 3. Use try/catch for error handling with await
122+
// 4. Use Promise.all for parallel operations
123+
// 5. Never mix callbacks and promises in the same flow

‎basics/01_variables_types.js‎

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// ============================================================
2+
// Variables, Data Types, and Type Coercion
3+
// ============================================================
4+
// JavaScript's type system, variable declarations, and the
5+
// difference between let, const, and var.
6+
// ============================================================
7+
8+
// ------------------------------------------------------------
9+
// Variable Declarations — let, const, var
10+
// ------------------------------------------------------------
11+
12+
// let: block-scoped, reassignable
13+
let count = 0;
14+
count = 1; // allowed
15+
16+
// const: block-scoped, cannot be reassigned
17+
const PI = 3.14159;
18+
// PI = 3; // TypeError: Assignment to constant variable
19+
20+
// var: function-scoped, hoisted — avoid in modern JS
21+
var legacy = "old style";
22+
23+
// Rule: use const by default. Use let when you need to reassign.
24+
// Never use var in new code.
25+
26+
// ------------------------------------------------------------
27+
// Primitive Types
28+
// ------------------------------------------------------------
29+
30+
const num = 42; // number (integer and float are the same type)
31+
const decimal = 3.14; // number
32+
const text = "Hello"; // string
33+
const flag = true; // boolean
34+
const empty = null; // null (intentional absence)
35+
let notSet; // undefined (not yet assigned)
36+
const id = Symbol("id"); // symbol (unique identifier)
37+
const big = 9007199254740991n; // bigint (arbitrary precision)
38+
39+
console.log("=== Primitive Types ===");
40+
console.log(typeof num); // "number"
41+
console.log(typeof text); // "string"
42+
console.log(typeof flag); // "boolean"
43+
console.log(typeof empty); // "object" — this is a known JS quirk
44+
console.log(typeof notSet); // "undefined"
45+
console.log(typeof id); // "symbol"
46+
console.log(typeof big); // "bigint"
47+
48+
// ------------------------------------------------------------
49+
// Strings — Template Literals
50+
// ------------------------------------------------------------
51+
52+
const name = "Alice";
53+
const age = 30;
54+
55+
// Concatenation (old way)
56+
console.log("Name: " + name + ", Age: " + age);
57+
58+
// Template literal (preferred — backticks)
59+
console.log(`Name: ${name}, Age: ${age}`);
60+
console.log(`Next year: ${age + 1}`);
61+
62+
// Multi-line strings
63+
const multiLine = `
64+
This is line one.
65+
This is line two.
66+
`;
67+
68+
// Common string methods
69+
console.log("\n=== String Methods ===");
70+
console.log("hello".toUpperCase()); // "HELLO"
71+
console.log("Hello World".includes("World")); // true
72+
console.log("Hello".padStart(10, "-")); // "-----Hello"
73+
console.log(" spaces ".trim()); // "spaces"
74+
console.log("a,b,c".split(",")); // ["a", "b", "c"]
75+
76+
// ------------------------------------------------------------
77+
// Type Coercion — Implicit Conversion
78+
// ------------------------------------------------------------
79+
80+
console.log("\n=== Type Coercion ===");
81+
82+
// == (loose equality) performs type coercion
83+
console.log(5 == "5"); // true — string coerced to number
84+
console.log(0 == false); // true — false coerced to 0
85+
86+
// === (strict equality) does NOT coerce — always use this
87+
console.log(5 === "5"); // false — different types
88+
console.log(0 === false); // false
89+
90+
// String + number → string concatenation
91+
console.log("5" + 3); // "53" (number converted to string)
92+
93+
// Other operators → numeric conversion
94+
console.log("5" - 3); // 2 (string converted to number)
95+
console.log("5" * 2); // 10
96+
97+
// Rule: always use === and !== to avoid coercion bugs.
98+
99+
// ------------------------------------------------------------
100+
// Truthy and Falsy Values
101+
// ------------------------------------------------------------
102+
103+
console.log("\n=== Falsy Values ===");
104+
// These are ALL the falsy values in JS:
105+
// false, 0, -0, 0n, "", null, undefined, NaN
106+
107+
if (!"") console.log('Empty string is falsy');
108+
if (!0) console.log('Zero is falsy');
109+
if (!null) console.log('null is falsy');
110+
111+
// Everything else is truthy, including:
112+
// "0", " ", [], {}, -1
113+
if ("0") console.log('"0" is truthy (non-empty string)');
114+
if ([]) console.log('[] is truthy (empty array)');

0 commit comments

Comments
 (0)