From 8801204783987ca9b3e9bc95c65392ef070a71ab Mon Sep 17 00:00:00 2001 From: darkless456 Date: Wed, 13 Aug 2025 09:35:32 +0000 Subject: [PATCH] chore: update study note to bc21b1039cb17c7e2d3c974efcc3de8e65b0c2a6 --- blog/2025-08-13-interview-tips-2025-8.md | 322 ++++++++++++++++++ .../stenography/weekly-fe-perusal/note-p3.md | 16 +- 2 files changed, 337 insertions(+), 1 deletion(-) create mode 100644 blog/2025-08-13-interview-tips-2025-8.md diff --git a/blog/2025-08-13-interview-tips-2025-8.md b/blog/2025-08-13-interview-tips-2025-8.md new file mode 100644 index 0000000..d3272fb --- /dev/null +++ b/blog/2025-08-13-interview-tips-2025-8.md @@ -0,0 +1,322 @@ +--- +title: Interview Tips +description: Interview Tips that generated by ChatGPT 5 +tags: [javascript, node.js, interview] +hide_table_of_contents: false +--- + +## JavaScript Core + +1) What is a closure? +- A closure is a function that remembers its outer scope via the **lexical environment**, even after the outer function returns. +- If asked: It keeps access to variables where it was created. Example: `function outer(){ let x=1; return function(){ console.log(x) } }`. + +2) Hoisting in JavaScript? +- **Declarations** are moved to the top; **var** hoists with undefined, **let/const** hoist but stay in **TDZ**. +- If asked: `var` is usable before line but is `undefined`. `let/const` throw if used before declared. Example: `console.log(a); var a=1;` vs `console.log(b); let b=1;`. + +3) How does “this” work? +- **this** depends on **call site**: default, **method**, **call/apply/bind**, **new**, or **arrow** (lexical). + +- If asked: In a method, `this` is the object. With `call/apply/bind`, you set it. Arrow uses the outer `this`. Example: `obj.fn()` vs `fn.call(obj)`. + +4) Prototype vs class? +- **Prototype chain** is native inheritance; **class** is syntax sugar over prototypes. +- If asked: `class` makes prototype code look cleaner. Under the hood it still uses prototypes. Example: `class A{}` ≈ function + `A.prototype`. + +5) Event loop basics? +- JS is **single-threaded**; **event loop** runs **call stack**, then **microtasks** (Promises), then **macrotasks**. +- If asked: Promises run before timers in the same tick. Example order: sync → microtasks → timers. + +6) Promise vs async/await? +- **Promises** model async; **async/await** is syntax for **sequential** style with **try/catch**. +- If asked: `await` pauses inside async function. Use `try/catch` for errors. Example: `const x = await fetch()`. + +7) Debounce vs throttle? +- **Debounce** waits for silence; **Throttle** limits calls to a **rate**. +- If asked: Debounce: run after user stops typing. Throttle: run at most once per 100ms while scrolling. + +8) == vs ===? +- **===** checks **type + value**; **==** does **coercion** (avoid unless sure). +- If asked: `0 == false` is true; `0 === false` is false. Prefer `===` to avoid surprises. + +9) var vs let vs const? +- **var** is **function-scoped**; **let/const** are **block-scoped**; **const** blocks **reassignment**. +- If asked: Use `const` by default, `let` when you reassign, avoid `var`. Blocks are `{ ... }`. + +10) Arrow function differences? +- **Arrow** has **lexical this**, no **arguments**, not **constructible**. +- If asked: Don’t use arrow as constructors or methods needing their own `this`. + +11) Shallow vs deep copy? +- **Shallow** copies top level; **Deep** copies nested data (**references** matter). +- If asked: Shallow: `const b = {...a}`. Deep: use `structuredClone(a)` (modern) or a lib. + +12) How to avoid memory leaks? +- Clear **timers**, remove **listeners**, avoid **globals**, break heavy **closures**. +- If asked: Keep `addEventListener` balanced with `removeEventListener`. Clear intervals on unmount. + +13) What is currying? +- Turn a function of **n args** into **n functions** of one arg for **reuse**/**composition**. +- If asked: `sum(1)(2)` instead of `sum(1,2)`. Useful to preset arguments. + +14) Immutable data—why? +- **Immutability** reduces **side effects**, helps **debugging**/**change detection**. +- If asked: Don’t modify input. Return new arrays/objects. Example: `const b = a.map(x=>x)`. + +15) Module systems: CommonJS vs ES Modules? +- **CommonJS** is **sync** (Node legacy); **ESM** is **static**, supports **tree-shaking**. +- If asked: CJS: `const x = require('x')`. ESM: `import x from 'x'`. + +16) What is CORS? +- **Cross-Origin** control via **HTTP headers** for **browser** access across **origins**. +- If asked: Server must allow your origin with headers like `Access-Control-Allow-Origin`. + +17) How does garbage collection work? +- Uses **reachability**; **mark-and-sweep** removes **unreachable** objects. +- If asked: If nothing references an object, GC frees it automatically. + +18) for...in vs for...of? +- **for...in** iterates **keys**; **for...of** iterates **values** of **iterables**. +- If asked: Use `for...in` for object keys; `for...of` for arrays, strings, maps. + +19) How to handle errors in async code? +- Use **try/catch** with **async/await** or **.catch**; add **unhandledRejection** handler. +- If asked: Always handle promise rejections. Example: `await fn().catch(handle)` or `try { await fn() } catch(e){}`. + +20) What is event delegation? +- Bind one **listener** to a **parent** and use **bubbling** for **performance**. +- If asked: Listen on `