In JavaScript, the ________ function is often used for delaying the execution of a function in an asynchronous manner.
- setTimeout
- setInterval
- asyncWait
- delayFunction
In JavaScript, the setTimeout function is often used for delaying the execution of a function in an asynchronous manner. It schedules the execution of a function after a specified time delay, allowing for tasks like animations or asynchronous operations to be handled.
Arrow functions were introduced in ECMAScript _________.
- 5
- 6
- 2015
- 2018
Arrow functions were introduced in ECMAScript 2015 (ES6). ES6 brought many enhancements to JavaScript, and arrow functions were one of the notable additions. They offer a more concise syntax for defining functions and have lexical scoping for this, making them a valuable addition to modern JavaScript.
In a while loop, placing a ________ statement inside the loop can help prevent infinite loops by providing an explicit exit.
- Break
- Continue
- Return
- Stop
In a while loop, placing a "break" statement inside the loop can help prevent infinite loops by providing an explicit exit condition. When a certain condition is met, the "break" statement terminates the loop, allowing you to exit the loop when necessary and avoid infinite iterations.
What is the most common issue developers might face when working with closures and loops together?
- Variable hoisting
- Memory leaks
- Unexpected type coercion
- Event propagation
The most common issue when working with closures and loops together is the creation of memory leaks. This happens when closures inside loops capture references to variables that are continuously changing in the loop, preventing them from being garbage collected, and leading to increased memory consumption. It's crucial to understand and manage these cases to avoid performance problems.
Which API allows you to make non-simple requests to another domain in JavaScript, considering the Same-Origin Policy?
- XMLHttpRequest
- JSONP (JSON with Padding)
- CORS (Cross-Origin Resource Sharing)
- WebSocket
CORS (Cross-Origin Resource Sharing) is the API that allows you to make non-simple requests (such as those with custom headers or methods) to another domain in JavaScript while considering the Same-Origin Policy. XMLHttpRequest is an older alternative but has limitations compared to CORS. JSONP is a workaround technique, not an API, and WebSocket is used for full-duplex communication, not for cross-origin requests.
The concept of creating a single function or method that can operate on multiple types of objects is known as _________.
- Overloading
- Inheritance
- Polymorphism
- Abstraction
The concept of creating a single function or method that can operate on multiple types of objects is known as "Polymorphism." In JavaScript, polymorphism can be achieved through method overriding and dynamic method binding, enabling different objects to respond to the same method in a way that suits their specific type.
How can you implement a while loop without causing a browser to freeze in case of a long-running process?
- Use asynchronous code and setTimeout to break the loop into smaller chunks, allowing the browser to handle other tasks.
- Wrap the loop in a Web Worker to offload it to a separate thread.
- Increase the browser's JavaScript execution stack size to handle long-running loops.
- Use a while loop with a higher timeout value to prevent freezing.
To prevent the browser from freezing during a long-running while loop, you should use asynchronous code and setTimeout to break the loop into smaller chunks. This allows the browser to handle other tasks and prevents the UI from becoming unresponsive. This approach follows the principles of non-blocking and responsive UI in web development.
The _______ method returns a Promise that resolves or rejects as soon as one of the promises in an iterable resolves or rejects, with the value or reason from that promise.
- Promise.race()
- Promise.all()
- Promise.any()
- Promise()
The correct answer is Promise.race(). This method takes an iterable of promises and returns a new promise that settles as soon as one of the input promises resolves or rejects. It's useful when you want to perform an action as soon as any of several asynchronous tasks completes.
The initial version of JavaScript was created in just _________ days.
- 5 days
- 10 days
- 15 days
- 20 days
The initial version of JavaScript was created in just 10 days by Brendan Eich in September 1995. This rapid development was possible due to the language's concise design and its primary purpose of enhancing web page interactivity.
What is "event bubbling" in JavaScript?
- It refers to blowing up events like balloons.
- It is the process of propagating events from child elements to parent elements in the DOM.
- It is the automatic inflation of event objects.
- It is a debugging technique in JavaScript.
"Event bubbling" in JavaScript refers to the process where events that occur on child elements are also propagated or "bubbled" up to their parent elements in the Document Object Model (DOM). This allows you to capture events at higher levels in the DOM hierarchy. Understanding event bubbling is crucial for event handling in JavaScript.