diff --git a/interview-questions/01_hoisting.js b/interview-questions/01_hoisting.js new file mode 100644 index 00000000..0db1164c --- /dev/null +++ b/interview-questions/01_hoisting.js @@ -0,0 +1,20 @@ +// ❓ Question: What is hoisting in JavaScript? + +console.log(a); // ? +var a = 10; + +hoistedFunction(); // ? + +function hoistedFunction() { + console.log("I am hoisted!"); +} + +/* +🧩 Output: +undefined +I am hoisted! + +🧠 Explanation: +- Variable declarations using `var` are hoisted to the top but not their assignments (so `a` is undefined). +- Function declarations are fully hoisted — both definition and declaration. +*/ diff --git a/interview-questions/02_closures.js b/interview-questions/02_closures.js new file mode 100644 index 00000000..3274ff68 --- /dev/null +++ b/interview-questions/02_closures.js @@ -0,0 +1,25 @@ +// ❓ Question: What is a closure? Explain with example. + +function outer() { + let counter = 0; + return function inner() { + counter++; + console.log(counter); + }; +} + +const fn = outer(); +fn(); // 1 +fn(); // 2 +fn(); // 3 + +/* +🧩 Output: +1 +2 +3 + +🧠 Explanation: +- A closure is created when a function retains access to its parent scope even after that parent has finished executing. +- Here, `inner` remembers `counter` from `outer`. +*/ diff --git a/interview-questions/03_this_keyword.js b/interview-questions/03_this_keyword.js new file mode 100644 index 00000000..2d4fb2ba --- /dev/null +++ b/interview-questions/03_this_keyword.js @@ -0,0 +1,22 @@ +// ❓ Question: What does `this` refer to in different contexts? + +const obj = { + name: "Alice", + show: function () { + console.log(this.name); + }, +}; + +obj.show(); // ? +const fn = obj.show; +fn(); // ? + +/* +🧩 Output: +Alice +undefined + +🧠 Explanation: +- Inside `obj.show()`, `this` refers to `obj`. +- When detached (`fn()`), `this` refers to the global object (or undefined in strict mode). +*/ diff --git a/interview-questions/04_event_loop.js b/interview-questions/04_event_loop.js new file mode 100644 index 00000000..53966821 --- /dev/null +++ b/interview-questions/04_event_loop.js @@ -0,0 +1,22 @@ +// ❓ Question: What is the order of output? + +console.log("Start"); + +setTimeout(() => console.log("Timeout"), 0); + +Promise.resolve().then(() => console.log("Promise")); + +console.log("End"); + +/* +🧩 Output: +Start +End +Promise +Timeout + +🧠 Explanation: +- JS executes synchronous code first. +- Then, microtasks (Promises). +- Finally, macrotasks (setTimeout). +*/ diff --git a/interview-questions/05_async_await.js b/interview-questions/05_async_await.js new file mode 100644 index 00000000..918e83fc --- /dev/null +++ b/interview-questions/05_async_await.js @@ -0,0 +1,20 @@ +// ❓ Question: Explain async/await behavior. + +async function test() { + console.log("1"); + await Promise.resolve(); + console.log("2"); +} +test(); +console.log("3"); + +/* +🧩 Output: +1 +3 +2 + +🧠 Explanation: +- `await` pauses only the async function, not the whole script. +- The rest of the code (`console.log("3")`) runs first. +*/ diff --git a/interview-questions/06_object_comparison.js b/interview-questions/06_object_comparison.js new file mode 100644 index 00000000..3a7cb403 --- /dev/null +++ b/interview-questions/06_object_comparison.js @@ -0,0 +1,17 @@ +// ❓ Question: Why does object comparison return false? + +const a = { x: 1 }; +const b = { x: 1 }; + +console.log(a == b); // ? +console.log(a === b); // ? + +/* +🧩 Output: +false +false + +🧠 Explanation: +Objects are compared by reference, not by value. +Each `{ x: 1 }` creates a new object at a different memory address. +*/ diff --git a/interview-questions/07_type_coercion.js b/interview-questions/07_type_coercion.js new file mode 100644 index 00000000..d5968617 --- /dev/null +++ b/interview-questions/07_type_coercion.js @@ -0,0 +1,22 @@ +// ❓ Question: What will be the output? + +console.log(1 + "2" + "2"); +console.log(1 + +"2" + "2"); +console.log(1 + -"1" + "2"); +console.log(+"1" + "1" + "2"); +console.log("A" - "B" + "2"); +console.log("A" - "B" + 2); + +/* +🧩 Output: +122 +32 +02 +112 +NaN2 +NaN + +🧠 Explanation: +- JS automatically converts types in expressions. +- `"A" - "B"` gives NaN because strings can't be subtracted. +*/ diff --git a/interview-questions/08_function_scope.js b/interview-questions/08_function_scope.js new file mode 100644 index 00000000..6847f329 --- /dev/null +++ b/interview-questions/08_function_scope.js @@ -0,0 +1,24 @@ +// ❓ Question: What happens to variable scope? + +function test() { + var a = 10; + if (true) { + let b = 20; + var c = 30; + } + console.log(a); // ? + // console.log(b); // ? + console.log(c); // ? +} +test(); + +/* +🧩 Output: +10 +30 +Error if b is logged + +🧠 Explanation: +- `var` is function-scoped. +- `let` and `const` are block-scoped. +*/ diff --git a/interview-questions/09_promise_chaining.js b/interview-questions/09_promise_chaining.js new file mode 100644 index 00000000..05673d79 --- /dev/null +++ b/interview-questions/09_promise_chaining.js @@ -0,0 +1,24 @@ +// ❓ Question: Predict the output of chained Promises + +Promise.resolve() + .then(() => { + console.log("1"); + return "2"; + }) + .then((data) => { + console.log(data); + throw new Error("Something went wrong"); + }) + .catch(() => console.log("3")) + .then(() => console.log("4")); + +/* +🧩 Output: +1 +2 +3 +4 + +🧠 Explanation: +- `.catch()` handles the error and then continues with the next `.then()`. +*/ diff --git a/interview-questions/10_destructuring.js b/interview-questions/10_destructuring.js new file mode 100644 index 00000000..21758e43 --- /dev/null +++ b/interview-questions/10_destructuring.js @@ -0,0 +1,19 @@ +// ❓ Question: What is destructuring and how does it work? + +const user = { name: "Bob", age: 25, city: "Delhi" }; + +const { name, age } = user; +console.log(name, age); + +const arr = [10, 20, 30]; +const [x, , y] = arr; +console.log(x, y); + +/* +🧩 Output: +Bob 25 +10 30 + +🧠 Explanation: +Destructuring allows unpacking properties or array items into variables easily. +*/