Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions interview-questions/01_hoisting.js
Original file line number Diff line number Diff line change
@@ -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.
*/
25 changes: 25 additions & 0 deletions interview-questions/02_closures.js
Original file line number Diff line number Diff line change
@@ -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`.
*/
22 changes: 22 additions & 0 deletions interview-questions/03_this_keyword.js
Original file line number Diff line number Diff line change
@@ -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).
*/
22 changes: 22 additions & 0 deletions interview-questions/04_event_loop.js
Original file line number Diff line number Diff line change
@@ -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).
*/
20 changes: 20 additions & 0 deletions interview-questions/05_async_await.js
Original file line number Diff line number Diff line change
@@ -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.
*/
17 changes: 17 additions & 0 deletions interview-questions/06_object_comparison.js
Original file line number Diff line number Diff line change
@@ -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.
*/
22 changes: 22 additions & 0 deletions interview-questions/07_type_coercion.js
Original file line number Diff line number Diff line change
@@ -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.
*/
24 changes: 24 additions & 0 deletions interview-questions/08_function_scope.js
Original file line number Diff line number Diff line change
@@ -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.
*/
24 changes: 24 additions & 0 deletions interview-questions/09_promise_chaining.js
Original file line number Diff line number Diff line change
@@ -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()`.
*/
19 changes: 19 additions & 0 deletions interview-questions/10_destructuring.js
Original file line number Diff line number Diff line change
@@ -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.
*/