Javascript Interview Deck 6 Flashcards
Explain memoization and how it can be implemented in JavaScript:
Memoization is a programming technique used to optimize functions by caching the results of expensive function calls and returning the cached result when the same inputs occur again. It helps improve performance by avoiding redundant computations. In JavaScript, memoization can be implemented using closures and object properties to store cached results.
Discuss the concept of Event Bubbling and Event Capturing phases:
Event bubbling and event capturing are two phases of event propagation in the Document Object Model (DOM) when an event occurs on an element and its ancestors.
Event bubbling is the default behavior where the event starts from the target element and propagates up the DOM hierarchy to its ancestors.
Event capturing is the opposite behavior where the event starts from the top of the DOM hierarchy and propagates down to the target element.
Both phases allow multiple event listeners to be triggered during event propagation.
Discuss the differences between the “call,” “apply,” and “bind” methods:
call: The call method is used to invoke a function with a specified this context and arguments passed individually.
apply: The apply method is similar to call but takes an array-like object of arguments instead of individual arguments.
bind: The bind method creates a new function with a specified this context and, optionally, pre-defined arguments, without invoking it immediately. It is commonly used for setting the this context for a function permanently.
What are arrow functions, and how do they differ from regular functions?
Arrow functions are a concise syntax for writing anonymous functions in JavaScript introduced in ECMAScript 6 (ES6).
They have a shorter syntax compared to regular functions and do not bind their own this, arguments, super, or new.target. Instead, they lexically capture the this value from the surrounding code.
Arrow functions are commonly used for shorter function expressions and in scenarios where the context of this is important.
Describe the role of the “fetch” API in making asynchronous HTTP requests:
The fetch API is a modern JavaScript interface for making HTTP requests in a more flexible and powerful way than traditional XMLHttpRequest.
It provides a simple and promise-based interface for making network requests, allowing developers to fetch resources from a server and handle the response asynchronously using Promises.
The fetch API supports various HTTP methods, headers, and request/response formats, making it suitable for a wide range of use cases.
Discuss the role of the “webpack” module bundler in JavaScript development:
Webpack is a popular module bundler for JavaScript applications, commonly used in modern web development workflows.
It allows developers to bundle and optimize JavaScript files, along with other web assets like CSS, images, and fonts, into a single bundle for deployment.
Webpack provides features like code splitting, tree shaking, and hot module replacement, enabling efficient code organization, optimization, and development experience.
Explain the differences between “shallow copy” and “deep copy” in JavaScript:
Shallow copy: Shallow copying creates a new object or array and copies the references of the original object’s properties or elements. Changes to nested objects or arrays in the shallow copy will affect the original object.
Deep copy: Deep copying creates a new object or array and recursively copies all nested objects or arrays, creating entirely new copies. Changes to nested objects or arrays in the deep copy will not affect the original object.
Discuss the advantages and disadvantages of using TypeScript over JavaScript:
Advantages: TypeScript offers advantages like static type checking, improved tooling and IDE support, better documentation through type annotations, enhanced code readability, and increased productivity through features like interfaces, generics, and advanced language features.
Disadvantages: TypeScript introduces additional complexity, requires compilation to JavaScript, may require a learning curve for developers unfamiliar with static typing, and may lead to overhead in terms of project setup and configuration.
What is the purpose of the “map,” “reduce,” and “filter” functions in JavaScript?
map: The map function is used to iterate over an array and transform each element into a new value based on a callback function. It returns a new array with the transformed values.
reduce: The reduce function is used to iterate over an array and accumulate a single value by applying a callback function to each element. It returns a single value representing the result of the accumulation.
filter: The filter function is used to iterate over an array and filter out elements based on a condition specified in a callback function. It returns a new array containing only the elements that pass the condition.
Explain the differences between “document.onload” and “DOMContentLoaded”:
document.onload: The document.onload event fires when the entire HTML document, including all external resources like images and stylesheets, has finished loading.
DOMContentLoaded: The DOMContentLoaded event fires when the initial HTML document has been completely loaded and parsed, without waiting for external resources like images and stylesheets. It allows scripts to be executed and manipulated the DOM earlier in the page loading process.
Describe the differences between “let,” “const,” and “var” for variable declaration:
var: var is function-scoped and allows variables to be redeclared within the same scope. It has a hoisting behavior where variable declarations are moved to the top of their containing function or global scope.
let: let is block-scoped and allows variables to be reassigned but not redeclared within the same scope. It does not have hoisting behavior, and variables declared with let are only accessible within the block they are declared in.
const: const is also block-scoped and requires variables to be initialized with a value at the time of declaration. It cannot be reassigned or redeclared within the same scope. Like let, const does not have hoisting behavior.
What is the purpose of the “IIFE” (Immediately Invoked Function Expression) pattern?
The IIFE pattern is a JavaScript design pattern used to create a function expression that is immediately executed after being defined. It provides a way to create a private scope for variables and functions, avoiding global namespace pollution and potential conflicts with other scripts. IIFEs are commonly used for encapsulating code and avoiding side effects.
Explain the concept of the “Event Loop” and how it relates to asynchronous programming:
The Event Loop is a mechanism in JavaScript responsible for handling asynchronous operations and ensuring non-blocking behavior. It continuously checks the call stack for functions to execute and the callback queue for tasks to be processed. The Event Loop enables asynchronous programming by managing the execution flow of asynchronous tasks, allowing JavaScript to handle multiple operations concurrently without blocking the main thread.
Describe the differences between “mutable” and “immutable” data structures in JavaScript:
Mutable data structures can be changed after they are created, allowing for modifications to their values or properties. Examples of mutable data structures in JavaScript include objects and arrays.
What will be the output of console.log(typeof NaN) and the reasoning behind it?
Output is ‘number’ because NaN (Not-a-Number) is of the type number.