JavaScript Flashcards
(12 cards)
Explain the difference between synchronous and asynchronous functions.
Synchronous functions are sequential and blocking. Asynchronous functions are non-blocking, e.g. promises.
Why is fetch the preferred way to send a network request via the web browser?
Because
1. It is natively supported in all modern browsers
2. It has a clean, simple syntax
3. It supports canceling with AbortSignal
What is one disadvantage of using fetch?
It does not reject on HTTP error status (e.g., 404, 500); you have to handle those manually
If the browser encounters malformed HTML, what happens?
- HTML Parser Steps In:
The browser’s HTML parser tries to fix or “guess” the intended structure using built-in rules defined in the HTML specification. - DOM Tree Is Built Anyway:
Even with errors, the browser still builds the DOM (Document Object Model), possibly inserting or moving elements to make the structure valid. - Rendering Continues:
The browser proceeds to render the page based on its best interpretation of the HTML, though the visual or functional outcome might not be what the developer intended.
Is there another option, besides explicit promises, to handle asynchronous code in JavaScript?
Yes, besides explicit Promises, JavaScript provides async/await as a modern and more readable way to handle asynchronous code.
Benefits Over Explicit Promises:
1. Cleaner syntax — avoids deeply nested .then() chains.
2. Better error handling — you can use try…catch just like in synchronous code.
3. Easier debugging — stack traces are easier to follow.
Explain the function and syntax of the array forEach() method.
The forEach() method in JavaScript is used to iterate over each element in an array and execute a function for every item.
array.forEach(callback(currentValue, index, array), thisArg);
Explain the function and syntax of the function bind() method.
The bind() method in JavaScript is used to create a new function with a specific this context, and optionally, preset arguments.
const boundFunction = originalFunction.bind(thisArg[, arg1, arg2, …]);
Is there a way to stop an event from bubbling up?
Yes, in JavaScript you can stop an event from bubbling up through the DOM by using the method:
event.stopPropagation()
How is the event loop defined in the JavaScript engine?
The event loop is the mechanism in the JavaScript engine that monitors the call stack and the task queues, and pushes asynchronous callbacks (like from setTimeout, fetch, DOM events, etc.) into the call stack when it’s empty, ensuring smooth execution of both synchronous and asynchronous code.
How do callbacks work in JavaScript?
A callback in JavaScript is a function passed as an argument to another function, and it’s intended to be invoked later, usually after some operation is complete.
They work by being:
1. Passed as an argument to another function.
2. Stored in memory just like any other value.
3. Invoked (called) at a later point inside the “host” function.
Have you worked with jQuery or other JS libraries actively?
Examples of other JavaScript Libraries / Frameworks
- React (with hooks, context, routing, testing)
- Vue.js (options & composition API)
- Angular (modules, services, observables)
- Lodash (utility functions for objects, arrays, etc.)
- D3.js (data visualizations)
- Moment.js / date-fns (date manipulation)
- Three.js (3D graphics in WebGL)
- RxJS (reactive programming with observables)
What design patterns do you know?
Classic Design Patterns Best Suited for React + Redux Front-End:
1. Observer Pattern: Objects (observers) subscribe to changes in a subject and get notified automatically, e.g. React components subscribe to Redux store changes
2. Facade Pattern: Provides a simplified interface to a complex subsystem, e.g. Abstract complex APIs, wrap Redux logic
3. Singleton Pattern: Ensures a class has only one instance, e.g. Single Redux store instance, single service instances
4. Command Pattern: Encapsulates a request as an object, allowing parameterization and queuing, e.g. Redux actions as commands, middleware intercepts
5. Proxy Pattern: Provides a placeholder or surrogate for another object to control access, e.g. Middleware/services intercepting calls/state access