# JSON, Regex & the Event Loop — JavaScript Fundamentals: From Syntax to Async

Source: https://www.skillbyai.com/en/javascript/js-json-regex-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.

```js
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.

```js
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.

**Quiz:** Why doesn't setTimeout(fn, 0) run fn immediately?

- [ ] Because 0ms actually means never
- [x] 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

**Quiz:** What does `typeof null` return?

- [ ] "null"
- [ ] "undefined"
- [x] "object"
- [ ] "number"

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

## Final quiz 2 of 8

Final quiz

**Quiz:** Which declaration is block-scoped and cannot be reassigned?

- [ ] var
- [ ] let
- [ ] function
- [x] const

*Answer:* const. const is block-scoped and fixes the binding.

## Final quiz 3 of 8

Final quiz

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

- [x] [2, 4, 6]
- [ ] 6
- [ ] undefined
- [ ] [1, 2, 3]

*Answer:* [2, 4, 6]. map returns a new array.

## Final quiz 4 of 8

Final quiz

**Quiz:** Which operator falls back only on null or undefined?

- [ ] ||
- [x] ??
- [ ] &&
- [ ] ==

*Answer:* ??. || also falls back on 0 and empty strings.

## Final quiz 5 of 8

Final quiz

**Quiz:** What is a closure?

- [ ] A way to close a browser tab
- [ ] A loop
- [x] 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

**Quiz:** What does event.preventDefault() do?

- [ ] Stops bubbling
- [ ] Removes the listener
- [ ] Reloads the page
- [x] 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

**Quiz:** What does `await` do inside an async function?

- [x] 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

**Quiz:** Why does setTimeout(fn, 0) not run immediately?

- [ ] 0 means never
- [x] 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.
