Timers, saving data, asynchronous code, real APIs and modern ES6+ syntax used in every professional project.
Every example opens in a live browser preview. Click ✎ Edit on any preview, or press L, to open the Code Lab.
| Function | Does |
|---|---|
setTimeout(fn, ms) | run once after ms |
setInterval(fn, ms) | run every ms, forever |
clearInterval(id) | stop an interval |
clearTimeout(id) | cancel a timeout before it runs |
if (timerId) return;"hi-IN" prints weekday and month names in Hindi automatically.Illustrative: rAF syncs with the screen refresh and pauses in hidden tabs.
{ hour12: false }. Add a greeting (Good morning/evening). Show the date in Hindi with "hi-IN".String(n).padStart(2, "0")Module 14: localStorage – make your apps remember data after a refresh.
Q1. What prints first?setTimeout(() => log("A"), 0); log("B");
Q2. How to stop const id = setInterval(f, 1000)?
Q3. new Date(2026, 0, 26) is:
| Method | Does |
|---|---|
setItem(k, v) | save (value becomes text) |
getItem(k) | read, or null |
removeItem(k) | delete one |
clear() | delete everything for this site |
localStorage.setItem("k", JSON.stringify(data))JSON.parse(localStorage.getItem("k")) || default| localStorage | sessionStorage | Cookies | |
|---|---|---|---|
| Lasts | until deleted | until the tab closes | until expiry date |
| Size | ~5 MB | ~5 MB | ~4 KB |
| Sent to server? | no | no | yes, with every request |
| Good for | theme, cart, drafts, notes | one-time form steps | login sessions (set by server) |
| Read with | localStorage.getItem | sessionStorage.getItem | document.cookie |
removeItem. Show a word count.Date.now() is a handy unique id: the number of milliseconds since 1970.Module 15: Async JavaScript – callbacks, promises, async/await and the event loop.
Q1. localStorage.getItem("age") after setItem("age", 15) returns:
Q2. How do you save an array?
Q3. Should passwords be saved in localStorage?
.then can return a new promise; the next .then waits for it.catch at the end handles an error from ANY step| Rule | |
|---|---|
async before function | it always returns a promise |
await promise | pause THIS function until it settles |
| Errors | use try...catch |
| Only inside async | (or at the top level of modules) |
Promise.race = first to finish wins (timeouts). Promise.allSettled = wait for all even if some fail.delay(ms) that returns a promise, and use it to count down 3, 2, 1, Go! for a quiz start screen.const delay = ms => new Promise(r => setTimeout(r, ms))pay(amount) returns a PromiseModule 16: Fetch & APIs – load real data from servers.
Q1. Output order of log(1); setTimeout(()=>log(2),0); log(3)?
Q2. Where can you use await?
Q3. A rejected promise is handled with:
/api/students, /api/products, /api/weather?city=ranchi, /api/quiz, /api/posts, /api/users/1, /api/error, /api/slow. It works offline.fetch(url) returns a promise of a Responseawait it to get status and headersawait response.json() to read the dataawait gives you Promise {<pending>} instead of data.| Code | Means | Show the user |
|---|---|---|
| 200 OK | success | the data |
| 201 Created | saved (after POST) | "Saved!" |
| 400 Bad Request | wrong input | fix the form |
| 401 / 403 | not logged in / not allowed | login page |
| 404 Not Found | no such thing | "Not found" |
| 500 Server Error | server problem | "Try again later" |
res.ok.| Method | Used for |
|---|---|
| GET | read data (default) |
| POST | create: sign up, place order, send message |
| PUT / PATCH | update: edit profile |
| DELETE | remove: delete a post |
id and status 201./api/students and show names with marks. Try /api/error to see the error message./api/weather?city=NAME; try Ranchi, Dhanbad, Netarhat, Patna).encodeURIComponent/api/students and show a table sorted by marks, with a "topper" highlight./api/products?q=TEXT), but wait 400 ms after the user stops typing before calling the server./api/feedback with POST, then show the saved record returned by the server./api/quiz and run a one-question-at-a-time quiz with score.Module 17: Modern ES6+ syntax and error handling.
Q1. What does await res.json() give?
Q2. A 404 response makes fetch:
Q3. To create a new record on a server use:
| Where | Name | Does |
|---|---|---|
| In a call / array / object literal | spread | opens the box |
| In parameters / destructuring | rest | packs the leftovers |
?. stops the crash; ?? gives a sensible default.|| replaces 0, "" and false too. ?? only replaces null and undefined.| Built-in error | Typical cause |
|---|---|
| ReferenceError | variable not defined |
| TypeError | wrong type, null.something |
| SyntaxError | bad code or bad JSON |
| RangeError | number out of range |
class ... extends Error is explained in Module 18.address?.city and read the error. Add a user with no name and default it to "Guest". Use spread to add role: "student" to every user.|| it would wrongly show "-"; ?? keeps the 0.Part 4: Classes, OOP, modules, debugging, browser APIs and 5 capstone apps.
Q1. 0 ?? 10 gives:
Q2. user?.address?.city when address is null gives:
Q3. Which block always runs?
You have finished Part 3 of the Complete JavaScript Course.
Keep all your practice files in D:\js-practice. You will reuse them in the capstone apps.