# JSON, Regex और Event Loop — JavaScript Fundamentals: Syntax से Async तक

Source: https://www.skillbyai.com/hi/javascript/js-json-regex-loop

> JSON से data serialize करें, regular expressions से patterns match करें और event loop समझें।

## JSON.stringify और parse

`stringify` किसी JS मान को JSON string में बदलता है; `parse` इसका उल्टा करता है।

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

## बेसिक regex

`.test()` मैच जाँचता है और boolean लौटाता है; `.match()` असली मैच किया गया टेक्स्ट लौटाता है।

```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"
```

## एक नज़र में event loop

JS **एक ही thread** पर चलता है, पर असिंक्रोनस काम (timer, fetch) ब्राउज़र/Node को सौंप दिया जाता है, और उसका callback तभी queue में जाता है जब मौजूदा call stack खाली हो — इसलिए असिंक्रोनस कोड नहीं रुकता।

**Quiz:** setTimeout(fn, 0) fn को तुरंत क्यों नहीं चलाता?

- [ ] क्योंकि 0ms का मतलब असल में कभी नहीं है
- [x] क्योंकि यह fn को मौजूदा call stack खत्म होने के बाद चलाने के लिए queue करता है
- [ ] क्योंकि setTimeout पूरी तरह सिंक्रोनस है
- [ ] क्योंकि setTimeout deprecated है

*Answer:* क्योंकि यह fn को मौजूदा call stack खत्म होने के बाद चलाने के लिए queue करता है. इसका callback 0ms देरी के बावजूद call stack खाली होने का इंतज़ार करता है।

## अंतिम क्विज़ 1/8

अंतिम क्विज़

**Quiz:** `typeof null` क्या लौटाता है?

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

*Answer:* "object". एक पुरानी विचित्रता: null को object बताया जाता है।

## अंतिम क्विज़ 2/8

अंतिम क्विज़

**Quiz:** कौन-सा declaration block-scoped है और दोबारा assign नहीं हो सकता?

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

*Answer:* const. const block-scoped है और binding को स्थिर करता है।

## अंतिम क्विज़ 3/8

अंतिम क्विज़

**Quiz:** `[1,2,3].map(n => n * 2)` क्या लौटाता है?

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

*Answer:* [2, 4, 6]. map नया array लौटाता है।

## अंतिम क्विज़ 4/8

अंतिम क्विज़

**Quiz:** कौन-सा operator केवल null या undefined पर fallback देता है?

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

*Answer:* ??. || 0 और खाली strings पर भी fallback देता है।

## अंतिम क्विज़ 5/8

अंतिम क्विज़

**Quiz:** 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 अपने बनने के scope तक पहुँच बनाए रखते हैं।

## अंतिम क्विज़ 6/8

अंतिम क्विज़

**Quiz:** event.preventDefault() क्या करता है?

- [ ] Stops bubbling
- [ ] Removes the listener
- [ ] Reloads the page
- [x] Stops the browser's default action

*Answer:* Stops the browser's default action. उदाहरण के लिए यह form submit होने से रोकता है।

## अंतिम क्विज़ 7/8

अंतिम क्विज़

**Quiz:** async function में `await` क्या करता है?

- [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. इंतज़ार के दौरान बाकी code चलता रहता है।

## अंतिम क्विज़ 8/8

अंतिम क्विज़

**Quiz:** setTimeout(fn, 0) तुरंत क्यों नहीं चलता?

- [ ] 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. Event loop मौजूदा code के बाद queued callbacks चलाता है।
