3. What is the difference between null and undefined?
The value null represents the intentional absence of any object value, while undefined indicates that a variable has been declared but has not been assigned a value.
A closure is a function that retains access to variables from its outer scope even after the outer function has finished executing. It allows for data encapsulation and the creation of private variables.
The “==” operator compares values after performing type conversion, while the “===” operator compares both values and types without type conversion.
Event delegation is a technique in which a single event handler is attached to a common ancestor element instead of attaching multiple event handlers to individual child elements. This improves performance and simplifies event handling.
The “this” keyword refers to the object that owns the current code being executed. Its value is determined by how a function is called or how an object is constructed.
A callback function is a function passed as an argument to another function, which is then invoked inside the outer function. It allows for asynchronous programming and handling of events.
Event bubbling is the default behavior where an event triggered on a child element will also trigger on its parent elements. Event capturing is the opposite, where the event is captured on the parent element first and then propagated to the child elements.
The “bind” method is used to create a new function with a specific context (the value of “this”) and, optionally, pre-set arguments. It is commonly used for event handling and creating function aliases.
16. What is the “typeof” operator used for?
The “async” keyword is used to declare an asynchronous function, which allows the use of the “await” keyword inside it. “await” is used to pause the execution of an async function until a Promise is resolved, simplifying asynchronous code and avoiding callback hell.
A shallow copy creates a new object that references the original object’s properties, while a deep copy creates a new object with completely independent copies of all the original object’s properties.
The “map” method is used to create a new array by iterating over an existing array and applying a transformation function to each element. It returns a new array with the transformed values.
A promise is an object representing the eventual completion or failure of an asynchronous operation. It allows you to write asynchronous code in a more readable and manageable way.
25. How do you handle errors in promises?
Errors in promises can be handled using the “catch” method, which is called when a promise is rejected. Additionally, each promise has a “finally” method that is called regardless of whether the promise is resolved or rejected.
Currying is a technique where a function with multiple arguments is transformed into a sequence of functions, each taking one argument. It allows for partial function application and the creation of reusable function templates.
The “reduce” method is used to reduce an array to a single value by repeatedly applying a callback function on each element. It accumulates the results of the callback function into a single value.
“null” is an intentional absence of any object value, while “undefined” means that a variable has been declared but has not been assigned a value. “null” is an assigned value, while “undefined” is an unassigned value.
“let” and “const” are block-scoped variables introduced in ES6, while “var” is function-scoped. “let” allows reassigning values, “const” is a constant that cannot be reassigned, and “var” has some hoisting behavior.
Event propagation refers to the order in which events are handled when an event occurs on a DOM element. It consists of two phases: capturing (downward from the document root) and bubbling (upward from the target element).
33. What is the purpose of the “localStorage” and “sessionStorage” objects?
The “localStorage” and “sessionStorage” objects provide web developers with a way to store key-value pairs locally on a user’s browser. The “localStorage” data persists across sessions, while the “sessionStorage” data is cleared when the session ends.
The “forEach” method is used to iterate over an array and execute a callback function on each element. It is a convenient way to perform an operation on each item without using a traditional for loop.
The main difference is scoping. Variables declared with “let” have block scope, meaning they are only accessible within the block where they are defined. Variables declared with “var” have function scope and are accessible throughout the entire function.
Memoization is a technique used to optimize function performance by caching the results of expensive function calls and returning the cached result when the same inputs occur again. It improves efficiency by avoiding unnecessary calculations.
The “splice” method is used to change the contents of an array by removing, replacing, or adding elements at a specific position. It modifies the original array and returns an array containing the removed elements.
A generator function is a special type of function that can be paused and resumed during its execution. It allows you to generate a series of values over time, providing a simpler syntax compared to traditional iterators.
The “split” method is used to split a string into an array of substrings based on a specified separator. It is commonly used to parse strings or extract specific parts of a string.
42. What is the difference between a deep clone and a shallow clone of an object?
A deep clone creates a new object with completely independent copies of all the properties and nested objects of the original object. A shallow clone creates a new object with references to the same properties and nested objects as the original object.
43. Explain the concept of the event delegation pattern.
The event delegation pattern is a technique where a single event listener is attached to a parent element instead of attaching individual listeners to multiple child elements. The parent element handles the events of its children by leveraging event bubbling.
The “arguments” object is an array-like object that contains the parameters passed to a function. It allows you to access the arguments dynamically, even if the function signature does not explicitly define them.
47. Explain the concept of memoization and its benefits.
Memoization is a technique used to optimize expensive function calls by caching their results. When the same inputs occur again, the cached result is returned instead of re-computing the function. Memoization improves performance by reducing redundant calculations.
The “slice” method returns a shallow copy of a portion of an array without modifying the original array. The “splice” method changes the contents of an array by removing or replacing existing elements or adding new elements at a specific position.
Both “apply” and “call” are used to invoke a function with a specified context (the value of “this”). The “apply” method accepts arguments as an array, while the “call” method accepts arguments individually.
Written by – Piyush Patil