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)।
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 সব এর উপর দাঁড়িয়ে।
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 / bind | x |
| 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/exportstatic, tree-shaking সম্ভব;requiredynamic, Node এর পুরনো পদ্ধতি।
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 দুটোই পাওয়া যায়।
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.mappolyfill নিজে লেখো (live coding এ আসে)।interfacevstype,anyvsunknown, generic constraint কী?