11. What is a promise in JS?
A Promise represents a value that may be available now, later, or never.
let promise = new Promise((resolve, reject) => {
resolve("Success");
});Use
.then() for success and .catch() for errors. 🤝12. Explain async/await
async functions return promises. await pauses execution until the promise resolves. ▶️⏸️async function fetchData() {
const res = await fetch('url');
const data = await res.json();
console.log(data);
}13. What is the difference between call, apply, and bind?
- call(): Calls a function with a given
this and arguments. 🗣️- apply(): Same as call(), but takes arguments as an array. 📦
- bind(): Returns a new function with
this bound. 🔗func.call(obj, a, b);
func.apply(obj, [a, b]);
const boundFunc = func.bind(obj);
14. What is a prototype?
Each JS object has a hidden
[[Prototype]] that it inherits methods and properties from. Used for inheritance. 🧬15. What is prototypal inheritance?
An object can inherit directly from another object using its prototype chain.
const parent = { greet() { return "Hi"; } };
const child = Object.create(parent);
child.greet(); // "Hi"16. What is the use of ‘this’ keyword in JS?
this refers to the object from which the function was called. In arrow functions, it inherits from the parent scope. 🧐17. Explain the concept of scope in JS
Scope defines where variables are accessible. JS has:
- Global scope 🌍
- Function scope 🏛️
- Block scope (with
let, const) 🧱18. What is lexical scope?
A function can access variables from its outer (parent) scope where it was defined, not where it's called. 📚
19. What are higher-order functions?
Functions that take other functions as arguments or return them. 🎓
Examples:
map(), filter(), reduce()20. What is a pure function?
- No side effects 🚫
- Same output for the same input ✅
Example:
function add(a, b) {
return a + b;
}💬 Tap ❤️ for Part-3!