पाठ 39 / 40
JSON, Regex और Event Loop
JSON से data serialize करें, regular expressions से patterns match करें और event loop समझें।
JSON.stringify और parse
stringify किसी JS मान को JSON string में बदलता है; parse इसका उल्टा करता है।
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() असली मैच किया गया टेक्स्ट लौटाता है।
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 खाली हो — इसलिए असिंक्रोनस कोड नहीं रुकता।
त्वरित जाँच: setTimeout(fn, 0) fn को तुरंत क्यों नहीं चलाता?
- क्योंकि 0ms का मतलब असल में कभी नहीं है
- क्योंकि यह fn को मौजूदा call stack खत्म होने के बाद चलाने के लिए queue करता है
- क्योंकि setTimeout पूरी तरह सिंक्रोनस है
- क्योंकि setTimeout deprecated है
Answer
क्योंकि यह fn को मौजूदा call stack खत्म होने के बाद चलाने के लिए queue करता है — इसका callback 0ms देरी के बावजूद call stack खाली होने का इंतज़ार करता है।
अंतिम क्विज़ 1/8
अंतिम क्विज़
त्वरित जाँच: `typeof null` क्या लौटाता है?
- "null"
- "undefined"
- "object"
- "number"
Answer
"object" — एक पुरानी विचित्रता: null को object बताया जाता है।
अंतिम क्विज़ 2/8
अंतिम क्विज़
त्वरित जाँच: कौन-सा declaration block-scoped है और दोबारा assign नहीं हो सकता?
- var
- let
- function
- const
Answer
const — const block-scoped है और binding को स्थिर करता है।
अंतिम क्विज़ 3/8
अंतिम क्विज़
त्वरित जाँच: `[1,2,3].map(n => n * 2)` क्या लौटाता है?
- [2, 4, 6]
- 6
- undefined
- [1, 2, 3]
Answer
[2, 4, 6] — map नया array लौटाता है।
अंतिम क्विज़ 4/8
अंतिम क्विज़
त्वरित जाँच: कौन-सा operator केवल null या undefined पर fallback देता है?
- ||
- ??
- &&
- ==
Answer
?? — || 0 और खाली strings पर भी fallback देता है।
अंतिम क्विज़ 5/8
अंतिम क्विज़
त्वरित जाँच: 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 अपने बनने के scope तक पहुँच बनाए रखते हैं।
अंतिम क्विज़ 6/8
अंतिम क्विज़
त्वरित जाँच: event.preventDefault() क्या करता है?
- Stops bubbling
- Removes the listener
- Reloads the page
- Stops the browser's default action
Answer
Stops the browser's default action — उदाहरण के लिए यह form submit होने से रोकता है।
अंतिम क्विज़ 7/8
अंतिम क्विज़
त्वरित जाँच: async function में `await` क्या करता है?
- 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
अंतिम क्विज़
त्वरित जाँच: setTimeout(fn, 0) तुरंत क्यों नहीं चलता?
- 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 — Event loop मौजूदा code के बाद queued callbacks चलाता है।