Lesson 39 / 40

JSON, Regex & the Event Loop

Serialize data with JSON, match patterns with regular expressions and understand the event loop.

JSON.stringify & parse

stringify turns a JS value into a JSON string; parse does the reverse.

const user = { name: "Ada", age: 36 };
const json = JSON.stringify(user);
console.log(json);              // '{"name":"Ada","age":36}'
console.log(JSON.parse(json));  // back to an object

Basic regex

.test() checks for a match and returns a boolean; .match() returns the actual matched text.

const pattern = /^\d{3}-\d{4}$/;
console.log(pattern.test("555-1234"));       // true
console.log("Call 555-1234 now".match(/\d{3}-\d{4}/)[0]);  // "555-1234"

The event loop, at a glance

JS runs on one thread, but async work (timers, fetch) is handed off to the browser/Node, and its callback is queued to run only after the current call stack empties — that's why async code doesn't block.

Quick check: Why doesn't setTimeout(fn, 0) run fn immediately?

  • Because 0ms actually means never
  • Because it queues fn to run after the current call stack finishes
  • Because setTimeout is fully synchronous
  • Because setTimeout is deprecated
Answer

Because it queues fn to run after the current call stack finishes — Its callback still waits for the call stack to clear, even with a 0ms delay.

Final quiz 1 of 8

Final quiz

Quick check: What does `typeof null` return?

  • "null"
  • "undefined"
  • "object"
  • "number"
Answer

"object" — A long-standing quirk: null reports as object.

Final quiz 2 of 8

Final quiz

Quick check: Which declaration is block-scoped and cannot be reassigned?

  • var
  • let
  • function
  • const
Answer

const — const is block-scoped and fixes the binding.

Final quiz 3 of 8

Final quiz

Quick check: What does `[1,2,3].map(n => n * 2)` return?

  • [2, 4, 6]
  • 6
  • undefined
  • [1, 2, 3]
Answer

[2, 4, 6] — map returns a new array.

Final quiz 4 of 8

Final quiz

Quick check: Which operator falls back only on null or undefined?

  • ||
  • ??
  • &&
  • ==
Answer

?? — || also falls back on 0 and empty strings.

Final quiz 5 of 8

Final quiz

Quick check: What is a closure?

  • A way to close a browser tab
  • A loop
  • A function that remembers variables from where it was created
  • A module
Answer

A function that remembers variables from where it was created — Closures keep access to their creation scope.

Final quiz 6 of 8

Final quiz

Quick check: What does event.preventDefault() do?

  • Stops bubbling
  • Removes the listener
  • Reloads the page
  • Stops the browser's default action
Answer

Stops the browser's default action — For example it stops a form from submitting.

Final quiz 7 of 8

Final quiz

Quick check: What does `await` do inside an async function?

  • Pauses that function until the promise settles
  • Blocks the whole page
  • Cancels the promise
  • Runs code twice
Answer

Pauses that function until the promise settles — Other code keeps running while it waits.

Final quiz 8 of 8

Final quiz

Quick check: Why does setTimeout(fn, 0) not run immediately?

  • 0 means never
  • fn is queued until the call stack is clear
  • setTimeout is synchronous
  • It needs a promise
Answer

fn is queued until the call stack is clear — The event loop runs queued callbacks after the current code.