Top 30 JavaScript
Top 30 JavaScript
Solution: `let` and `const` are block-scoped, while `var` is function-scoped. `let` allows
reassignment, `const` does not, and `var` is hoisted without block scope.
Solution: Hoisting is JavaScript's default behavior of moving declarations to the top of their
containing scope. Only declarations are hoisted, not initializations.
Solution: A closure is when a function retains access to its lexical scope, even when executed
outside of that scope.
Code:
function outer() {
let count = 0;
count++;
return count;
};
console.log(increment()); // 1
console.log(increment()); // 2
```
Solution: `==` checks for value equality with type coercion, whereas `===` checks for both value and
type equality without coercion.
Code:
Top 30 JavaScript Questions with Answers
(function() {
console.log("IIFE executed!");
})();
```
Solution: `this` refers to the object that is executing the current function. It can change based on
the context: global, object method, or function.
Solution: Every JavaScript object has a prototype that acts as a template object from which it
inherits properties and methods.
8. What are arrow functions and how do they differ from regular functions?
Solution: Arrow functions are a concise syntax for writing functions. They do not have their own
`this` and are not suitable for use as methods.
Code:
```
Solution: Event delegation is a technique in which you attach a single event listener to a parent
element to handle events for all its child elements.
Solution: These methods allow you to set the `this` context of a function. `call()` and `apply()`
invoke functions immediately, while `bind()` returns a new function.
Code:
function greet() {
console.log(`Hello, ${this.name}`);
Solution: `setTimeout` executes a function after a delay, while `setInterval` repeatedly executes a
function at a specified interval.
Solution: Promises represent asynchronous operations. They can be in one of three states: pending,
resolved, or rejected.
Code:
});
```
Solution: `async/await` is syntax for handling promises more cleanly. An `async` function returns a
promise, and `await` pauses execution until the promise resolves.
Solution: `map()` returns a new array with the results of calling a function on every array element,
while `forEach()` executes the function without returning anything.
Solution: Template literals use backticks (`` ` ``) and allow embedded expressions and multi-line
strings.
Code:
console.log(`Hello, ${name}`);
```
Solution: Destructuring is a way of unpacking values from arrays or properties from objects.
Code:
```
Solution: The spread operator (`...`) spreads elements of an array or object, while the rest operator
collects elements into an array.
Code:
```
Solution: `NaN` stands for "Not-a-Number". You can check it using `Number.isNaN()` or `isNaN()`.
Code:
console.log(Number.isNaN(NaN)); // true
```
Solution: The event loop is a mechanism that handles the execution of JavaScript code, allowing
non-blocking I/O operations.
Solution: A promise chain is a sequence of `.then()` calls that execute asynchronously one after the
other.
Code:
Top 30 JavaScript Questions with Answers
fetchData()
.then(processData)
.then(displayData)
.catch(handleError);
```
Solution: JSON (JavaScript Object Notation) is a format for data. You use `JSON.parse()` to convert a
JSON string to a JavaScript object and `JSON.stringify()` to convert an object to a JSON string.
Solution: The Document Object Model (DOM) is an interface that allows scripts to access and
manipulate the content, structure, and style of a webpage.
Code:
```
Solution: Higher-order functions are functions that take other functions as arguments or return
them.
Code:
console.log(operation(add, 5, 3)); // 8
```
Top 30 JavaScript Questions with Answers
26. Explain currying.
Solution: Currying transforms a function with multiple arguments into a series of functions that
each take a single argument.
Code:
console.log(multiply(2)(3)); // 6
```
Solution: Memoization is an optimization technique where results of expensive function calls are
cached.
Code:
console.log('Computing...');
cache[n] = n + 10;
return cache[n];
};
})();
```
Solution: Functional programming is a paradigm where functions are first-class citizens, promoting
immutability and pure functions.
Solution: JavaScript modules allow code to be divided into separate files and imported or exported
as needed, enabling better organization.
Code:
let timer;
return function () {
clearTimeout(timer);
};
};