Lesson 38 / 38

JSON, Regex & the Event Loop

Serializing data, matching patterns, and why async doesn't block.

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 it queues fn to run after the current call stack finishes
  • Because 0ms actually means never
  • Because setTimeout is fully synchronous
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.