অধ্যায় ০৩ / ১৮Fundamentals

JavaScript ও TypeScript Deep Dive

Event loop থেকে generics — ভিতটা করো আরও মজবুত।

Framework বদলায়, কিন্তু JavaScript এর core concept বদলায় না। Interview এ সবচেয়ে বেশি আসে: event loop, closure, this, promise, prototype আর hoisting।

Event Loop

JavaScript single-threaded। Call stack এ synchronous code চলে; async কাজ শেষ হলে callback queue তে যায়। Stack খালি হলে আগে সব microtask (Promise .then, queueMicrotask) চলে, তারপর একটা macrotask (setTimeout, I/O)।

js
console.log("1");
setTimeout(() => console.log("2"), 0);
Promise.resolve().then(() => console.log("3"));
console.log("4");
// Output: 1, 4, 3, 2

Closure

Function তার তৈরির সময়ের scope এর variable মনে রাখে, একেই বলে closure। Private state, memoization, debounce, React hooks সব এর উপর দাঁড়িয়ে।

js
function counter() {
  let count = 0;
  return () => ++count;
}
const next = counter();
next(); next(); // 2

// Classic trap: var এর function scope
for (var i = 0; i < 3; i++) setTimeout(() => console.log(i)); // 3 3 3
for (let j = 0; j < 3; j++) setTimeout(() => console.log(j)); // 0 1 2

this এর নিয়ম

কীভাবে call হলোthis কী
obj.method()obj
সাধারণ fn()strict mode এ undefined
new Fn()নতুন object
fn.call(x) / apply / bindx
Arrow functionবাইরের scope এর this (নিজস্ব this নেই)

Promise আর async/await

  • Promise.all: সবগুলো সফল হলে তবেই; একটা fail করলে পুরোটা fail।
  • Promise.allSettled: সবগুলো শেষ হওয়ার অপেক্ষা, ফলাফল যাই হোক।
  • Promise.race: প্রথমটা শেষ হওয়া মাত্র (timeout বানাতে কাজের)। Promise.any: প্রথম সফলটা।
  • Loop এর ভেতরে একটা একটা করে await করলে কাজ sequential হয়; independent কাজ হলে Promise.all দিয়ে parallel চালাও।

অন্যান্য জরুরি concept

  • Hoisting: var আর function declaration উপরে উঠে যায়; let/const এর temporal dead zone আছে।
  • Prototype chain: প্রতিটা object এর [[Prototype]] থাকে; property না পেলে chain বেয়ে খোঁজে। class আসলে এরই syntactic sugar।
  • Equality: সবসময় ===; == type coercion করে (0 == "" হলো true)।
  • Value vs reference: object/array reference হিসেবে যায়। Spread (...) shallow copy; deep copy এর জন্য structuredClone()।
  • Debounce vs throttle: debounce শেষ event এর পর অপেক্ষা করে (search box); throttle নির্দিষ্ট সময়ে সর্বোচ্চ একবার চালায় (scroll)।
  • ES Modules vs CommonJS: import/export static, tree-shaking সম্ভব; require dynamic, Node এর পুরনো পদ্ধতি।
js
function debounce(fn, ms) {
  let t;
  return (...args) => {
    clearTimeout(t);
    t = setTimeout(() => fn(...args), ms);
  };
}

TypeScript essentials

TypeScript compile-time এ type check করে, runtime এ plain JavaScript চলে। Type system structural: নাম না, shape মিললেই compatible।

  • interface বনাম type: object এর জন্য interface (extend, merge করা যায়), union/tuple এর জন্য type।
  • unknown বনাম any: বাইরের data সবসময় unknown, তারপর validate করে narrow।
  • Union + narrowing, discriminated union, generics (<T extends ...>), utility types (Partial, Pick, Omit, Record, ReturnType)।
  • tsconfig.json এ সবসময় "strict": true।
  • Runtime validation এর জন্য Zod: schema থেকে type আর validator দুটোই পাওয়া যায়।
ts
type ApiState<T> =
  | { status: "loading" }
  | { status: "success"; data: T }
  | { status: "error"; error: string };

const User = z.object({ id: z.number(), email: z.string().email() });
type User = z.infer<typeof User>;

Interview প্রশ্ন

  • Event loop বোঝাও; microtask আর macrotask এর পার্থক্য কী? উপরের code এর output কী?
  • Closure কী? একটা real use case বলো। var দিয়ে loop এর সমস্যা কেন হয়?
  • call, apply, bind এর পার্থক্য? Arrow function এ this কেমন?
  • debounce, Promise.all, deep clone, Array.prototype.map polyfill নিজে লেখো (live coding এ আসে)।
  • interface vs type, any vs unknown, generic constraint কী?

এই অধ্যায় পড়া শেষ?

পড়া শেষ চিহ্নিত করো, নিজের অগ্রগতি ধরে রাখো।