Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e44028a

Browse files
tahachmgithub-actions[bot]
andauthoredApr 18, 2025··
qns: address user feedback (#29)
Co-authored-by: GitHub Actions <github-actions[bot]@users.noreply.github.com>
1 parent cefc4b0 commit e44028a

File tree

17 files changed

+87
-40
lines changed
  • questions
    • describe-event-bubbling
    • explain-event-delegation
    • explain-hoisting
    • explain-the-concept-of-a-callback-function-in-asynchronous-operations
    • how-do-you-abort-a-web-request-using-abortcontrollers
    • how-do-you-handle-errors-in-asynchronous-operations
    • how-do-you-validate-form-elements-using-the-constraint-validation-api
    • what-are-iterators-and-generators-and-what-are-they-used-for
    • what-are-some-common-performance-bottlenecks-in-javascript-applications
    • what-are-the-differences-between-xmlhttprequest-and-fetch
    • what-are-the-pros-and-cons-of-using-promises-instead-of-callbacks
    • what-is-the-definition-of-a-higher-order-function
    • what-is-the-difference-between-double-equal-and-triple-equal
    • what-is-the-difference-between-mouseenter-and-mouseover-event
    • whats-the-difference-between-a-variable-that-is-null-undefined-or-undeclared-how-would-you-go-about-checking-for-any-of-these-states
    • whats-the-difference-between-call-and-apply

17 files changed

+87
-40
lines changed
 

‎README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,7 @@ The main difference lies in the bubbling behavior of `mouseenter` and `mouseover
10641064

10651065
`mouseenter` events do not bubble. The `mouseenter` event is triggered only when the mouse pointer enters the element itself, not its descendants. If a parent element has child elements, and the mouse pointer enters child elements, the `mouseenter` event will not be triggered on the parent element again, it's only triggered once upon entry of parent element without regard for its contents. If both parent and child have `mouseenter` listeners attached and the mouse pointer moves from the parent element to the child element, `mouseenter` will only fire for the child.
10661066

1067-
`mouseover` events bubble up the DOM tree. The `mouseover` event is triggered when the mouse pointer enters the element or one of its descendants. If have a parent element has child elements, and the mouse pointer enters child elements, the `mouseover` event will be triggered on the parent element again as well. If the parent element has multiple child elements, this can result in multiple event callbacks fired. If there are child elements, and the mouse pointer moves from the parent element to the child element, `mouseover` will fire for both the parent and the child.
1067+
`mouseover` events bubble up the DOM tree. The `mouseover` event is triggered when the mouse pointer enters the element or one of its descendants. If a parent element has child elements, and the mouse pointer enters child elements, the `mouseover` event will be triggered on the parent element again as well. If the parent element has multiple child elements, this can result in multiple event callbacks fired. If there are child elements, and the mouse pointer moves from the parent element to the child element, `mouseover` will fire for both the parent and the child.
10681068

10691069
| Property | `mouseenter` | `mouseover` |
10701070
| --- | --- | --- |
@@ -4921,7 +4921,7 @@ The main difference lies in the bubbling behavior of `mouseenter` and `mouseover
49214921

49224922
`mouseenter` events do not bubble. The `mouseenter` event is triggered only when the mouse pointer enters the element itself, not its descendants. If a parent element has child elements, and the mouse pointer enters child elements, the `mouseenter` event will not be triggered on the parent element again, it's only triggered once upon entry of parent element without regard for its contents. If both parent and child have `mouseenter` listeners attached and the mouse pointer moves from the parent element to the child element, `mouseenter` will only fire for the child.
49234923

4924-
`mouseover` events bubble up the DOM tree. The `mouseover` event is triggered when the mouse pointer enters the element or one of its descendants. If have a parent element has child elements, and the mouse pointer enters child elements, the `mouseover` event will be triggered on the parent element again as well. If the parent element has multiple child elements, this can result in multiple event callbacks fired. If there are child elements, and the mouse pointer moves from the parent element to the child element, `mouseover` will fire for both the parent and the child.
4924+
`mouseover` events bubble up the DOM tree. The `mouseover` event is triggered when the mouse pointer enters the element or one of its descendants. If a parent element has child elements, and the mouse pointer enters child elements, the `mouseover` event will be triggered on the parent element again as well. If the parent element has multiple child elements, this can result in multiple event callbacks fired. If there are child elements, and the mouse pointer moves from the parent element to the child element, `mouseover` will fire for both the parent and the child.
49254925

49264926
| Property | `mouseenter` | `mouseover` |
49274927
| --- | --- | --- |

‎questions/describe-event-bubbling/en-US.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ child.click();
8585

8686
## Event delegation
8787

88-
Event bubbling is the basis for a technique called [event delegation](/questions/quiz/describe-event-delegation), where you attach a single event handler to a common ancestor of multiple elements and use event delegation to handle events for those elements efficiently. This is particularly useful when you have a large number of similar elements, like a list of items, and you want to avoid attaching individual event handlers to each item.
88+
Event bubbling is the basis for a technique called [event delegation](/questions/quiz/explain-event-delegation), where you attach a single event handler to a common ancestor of multiple elements and use event delegation to handle events for those elements efficiently. This is particularly useful when you have a large number of similar elements, like a list of items, and you want to avoid attaching individual event handlers to each item.
8989

9090
```js
9191
parent.addEventListener('click', (event) => {

‎questions/explain-event-delegation/en-US.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ userForm.addEventListener('input', (event) => {
108108
});
109109
```
110110

111-
In this example, a single input event listener is attached to the form element. It can respond to input changes for all child input elements, simplifying the code by an event listeners per `<input>` element.
111+
In this example, a single input event listener is attached to the form element. It can respond to input changes for all child input elements, simplifying the code by eliminating the need for individual listeners on each `<input>` element.
112112

113113
## Pitfalls
114114

‎questions/explain-hoisting/en-US.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,4 @@ ESLint is a static code analyzer that can find violations of such cases with the
140140
## Further reading
141141

142142
- [Hoisting | MDN](https://developer.mozilla.org/en-US/docs/Glossary/Hoisting)
143-
- [JavaScript Visualized: Hoisting](https://dev.to/lydiahallie/javascript-visualized-hoisting-478h)
143+
- [What is Hoisting in JavaScript?](https://www.freecodecamp.org/news/what-is-hoisting-in-javascript)

‎questions/explain-the-concept-of-a-callback-function-in-asynchronous-operations/en-US.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,4 @@ fetchData((error, data) => {
9696

9797
- [MDN Web Docs: Callback function](https://developer.mozilla.org/en-US/docs/Glossary/Callback_function)
9898
- [JavaScript.info: Callbacks](https://javascript.info/callbacks)
99-
- [Node.js: Asynchronous programming and callbacks](https://nodejs.org/en/knowledge/getting-started/control-flow/what-are-callbacks/)
99+
- [Node.js: Asynchronous programming and callbacks](https://nodejs.org/en/learn/asynchronous-work/javascript-asynchronous-programming-and-callbacks)

‎questions/how-do-you-abort-a-web-request-using-abortcontrollers/en-US.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ In situations where the user has navigated away from the page, aborting the requ
153153
## Notes
154154

155155
- `AbortController`s is not `fetch()`-specific, it can be used to abort other asynchronous tasks as well.
156-
- A singular `AbortContoller` instance can be reused or multiple async tasks and cancel all of them at once.
156+
- A singular `AbortContoller` instance can be reused on multiple async tasks and cancel all of them at once.
157157
- Calling `abort()` on `AbortController`s does not send any notification or signal to the server. The server is unaware of the cancelation and will continue processing the request until it completes or times out.
158158

159159
## Further reading

‎questions/how-do-you-handle-errors-in-asynchronous-operations/en-US.mdx

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,28 +58,33 @@ fetchData(); // Error fetching data: ....
5858
If you have multiple asynchronous operations, you can nest `try...catch` blocks to handle errors at different levels.
5959

6060
```js live
61-
async function fetchData() {
62-
try {
63-
// Invalid URl
64-
const response = await fetch('https://api.example.com/data');
65-
const data = await response.json();
66-
console.log(data);
67-
} catch (error) {
68-
console.error('Error fetching data:', error);
69-
}
61+
async function fetchUser() {
62+
// Simulate a successful async operation
63+
return { id: 1, name: 'Alice' };
7064
}
7165

72-
async function processData() {
66+
async function fetchUserPosts() {
67+
// Simulate a failed async operation
68+
throw new Error('Failed to fetch posts');
69+
}
70+
71+
async function loadUserData() {
7372
try {
74-
await fetchData();
75-
// Additional processing
76-
console.log(arr); // Trying to reference an undefined variable will throw an error
77-
} catch (error) {
78-
console.error('Error processing data:', error);
73+
const user = await fetchUser();
74+
console.log('User:', user);
75+
76+
try {
77+
const posts = await fetchUserPosts();
78+
console.log('Posts:', posts);
79+
} catch (postsError) {
80+
console.error('Error fetching posts:', postsError.message);
81+
}
82+
} catch (userError) {
83+
console.error('Error fetching user:', userError.message);
7984
}
8085
}
8186

82-
processData();
87+
loadUserData();
8388
```
8489

8590
## Using `.catch()` with Promises

‎questions/how-do-you-validate-form-elements-using-the-constraint-validation-api/en-US.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,4 @@ In this example, the form will not submit if the `username` input is empty, and
115115

116116
- [MDN Web Docs: Constraint Validation](https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Constraint_validation)
117117
- [MDN Web Docs: HTMLFormElement.checkValidity()](https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/checkValidity)
118-
- [MDN Web Docs: HTMLFormElement.setCustomValidity()](https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/setCustomValidity)
118+
- [MDN Web Docs: HTMLObjectElement.setCustomValidity()](https://developer.mozilla.org/en-US/docs/Web/API/HTMLObjectElement/setCustomValidity)

‎questions/what-are-iterators-and-generators-and-what-are-they-used-for/en-US.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ In JavaScript, several built-in objects implement the iterator protocol, meaning
179179

180180
Generators are a special kind of function that can pause and resume their execution, allowing them to generate a sequence of values on-the-fly. They are commonly used to create iterators but have other applications as well. The key use cases of generators include:
181181

182-
- Creating iterators is a more concise and readable way compared to manually implementing the iterator protocol.
182+
- Creating iterators in a more concise and readable way compared to manually implementing the iterator protocol.
183183
- Implementing lazy evaluation, where values are generated only when needed, saving memory and computation time.
184184
- Simplifying asynchronous programming by allowing code to be written in a synchronous-looking style using `yield` and `await`.
185185

‎questions/what-are-some-common-performance-bottlenecks-in-javascript-applications/en-US.mdx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,22 @@ Layout thrashing occurs when you read and write to the DOM repeatedly, causing m
3838

3939
```js
4040
// Inefficient
41-
for (let i = 0; i < 1000; i++) {
42-
const height = element.clientHeight;
43-
element.style.height = `${height + 10}px`;
44-
}
41+
boxes.forEach((box) => {
42+
const height = box.offsetHeight; // Read
43+
box.style.height = `${height + 10}px`; // Write
44+
});
4545

4646
// Efficient
47-
const height = element.clientHeight;
48-
for (let i = 0; i < 1000; i++) {
49-
element.style.height = `${height + 10}px`;
50-
}
47+
// Batch read
48+
const heights = [];
49+
boxes.forEach((box) => {
50+
heights.push(box.offsetHeight);
51+
});
52+
53+
// Batch write
54+
boxes.forEach((box, i) => {
55+
box.style.height = `${heights[i] + 10}px`;
56+
});
5157
```
5258

5359
## Excessive use of global variables

‎questions/what-are-the-differences-between-xmlhttprequest-and-fetch/en-US.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ xhr.upload.onprogress = (event) => {
224224
The callback assigned to `onprogress` is passed a [`ProgressEvent`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/progress_event):
225225

226226
- The `loaded` field on the `ProgressEvent` is a 64-bit integer indicating the amount of work already performed (bytes uploaded/downloaded) by the underlying process.
227-
- The `total` field on the `ProgressEvent` is a 64-bit integer representing the total amount of work that the underlying process is in the progress of performing. When downloading resources, this is the `Content-Length` value of the HTTP request.
227+
- The `total` field on the `ProgressEvent` is a 64-bit integer representing the total amount of work that the underlying process is in the progress of performing. When downloading resources, this is the `Content-Length` value of the HTTP response.
228228

229229
On the other hand, the `fetch()` API does not offer any convenient way to track upload progress. It can be implemented by monitoring the `body` of the `Response` object as a fraction of the `Content-Length` header, but it's quite complicated.
230230

‎questions/what-are-the-pros-and-cons-of-using-promises-instead-of-callbacks/en-US.mdx

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,49 @@ function getData3() {
115115

116116
Promise.all([getData1(), getData2(), getData3()])
117117
.then((results) => {
118-
console.log(results); // Output: [[{ id: 1, title: 'Data 1' }, { id: 2, title: 'Data 2' }, { id: 3, title: 'Data 3' }]
118+
console.log(results); // Output: [{ id: 1, title: 'Data 1' }, { id: 2, title: 'Data 2' }, { id: 3, title: 'Data 3' }]
119119
})
120120
.catch((error) => {
121121
console.error('Error:', error);
122122
});
123123
```
124124

125+
### Easier error handling with `.catch()` and guaranteed cleanup with `.finally()`
126+
127+
Promises make error handling more straightforward by allowing you to catch errors at the end of a chain using `.catch()`, instead of manually checking for errors in every callback. This leads to cleaner and more maintainable code.
128+
129+
Additionally, `.finally()` lets you run code after the Promise settles, whether it was successful or failed, which is great for cleanup tasks like hiding spinners or resetting UI states.
130+
131+
```js live
132+
function getFirstData() {
133+
return new Promise((resolve) => {
134+
setTimeout(() => {
135+
resolve({ id: 1, title: 'First Data' });
136+
}, 1000);
137+
});
138+
}
139+
140+
function getSecondData(data) {
141+
return new Promise((resolve) => {
142+
setTimeout(() => {
143+
resolve({ id: data.id, title: data.title + ' -> Second Data' });
144+
}, 1000);
145+
});
146+
}
147+
148+
getFirstData()
149+
.then(getSecondData)
150+
.then((data) => {
151+
console.log('Success:', data);
152+
})
153+
.catch((error) => {
154+
console.error('Error:', error);
155+
})
156+
.finally(() => {
157+
console.log('This runs no matter what');
158+
});
159+
```
160+
125161
### With promises, these scenarios which are present in callbacks-only coding, will not happen:
126162

127163
- Call the callback too early

‎questions/what-is-the-definition-of-a-higher-order-function/en-US.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function greetName(greeter, name) {
5858
greetName(greet, 'Alice'); // Output: Hello, Alice!
5959
```
6060

61-
In this example, the `greetName` function takes another function `greet` as an argument and executes it with the name `Alice`. The `greet` function is a higher-order function because it is passed as an argument to another function.
61+
In this example, the `greetName` function is higher-order function because it takes another function (`greet`) as an argument and uses it to generate a greeting for the given name.
6262

6363
### Functions as return values
6464

‎questions/what-is-the-difference-between-double-equal-and-triple-equal/en-US.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ There's one final value-comparison operation within JavaScript, that is the [`Ob
7777

7878
## Conclusion
7979

80-
- Use `==` when you want to compare values with type coercion (and understand the implications of it). Practically, the only valid use case for the equality operator is when against `null` and `undefined` for convenience.
80+
- Use `==` when you want to compare values with type coercion (and understand the implications of it). In practice, the only reasonable use case for the equality operator is to check for both `null` and `undefined` in a single comparison for convenience.
8181
- Use `===` when you want to ensure both the value and the type are the same, which is the safer and more predictable choice in most cases.
8282

8383
### Notes

‎questions/what-is-the-difference-between-mouseenter-and-mouseover-event/en-US.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The main difference lies in the bubbling behavior of `mouseenter` and `mouseover
88

99
`mouseenter` events do not bubble. The `mouseenter` event is triggered only when the mouse pointer enters the element itself, not its descendants. If a parent element has child elements, and the mouse pointer enters child elements, the `mouseenter` event will not be triggered on the parent element again, it's only triggered once upon entry of parent element without regard for its contents. If both parent and child have `mouseenter` listeners attached and the mouse pointer moves from the parent element to the child element, `mouseenter` will only fire for the child.
1010

11-
`mouseover` events bubble up the DOM tree. The `mouseover` event is triggered when the mouse pointer enters the element or one of its descendants. If have a parent element has child elements, and the mouse pointer enters child elements, the `mouseover` event will be triggered on the parent element again as well. If the parent element has multiple child elements, this can result in multiple event callbacks fired. If there are child elements, and the mouse pointer moves from the parent element to the child element, `mouseover` will fire for both the parent and the child.
11+
`mouseover` events bubble up the DOM tree. The `mouseover` event is triggered when the mouse pointer enters the element or one of its descendants. If a parent element has child elements, and the mouse pointer enters child elements, the `mouseover` event will be triggered on the parent element again as well. If the parent element has multiple child elements, this can result in multiple event callbacks fired. If there are child elements, and the mouse pointer moves from the parent element to the child element, `mouseover` will fire for both the parent and the child.
1212

1313
| Property | `mouseenter` | `mouseover` |
1414
| --- | --- | --- |

‎questions/whats-the-difference-between-a-variable-that-is-null-undefined-or-undeclared-how-would-you-go-about-checking-for-any-of-these-states/en-US.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ console.log(typeof y === 'undefined'); // true
3434

3535
## `undefined`
3636

37-
A variable that is `undefined` is a variable that has been declared, but not assigned a value. It is of type `undefined`. If a function does not return any value as the result of executing it is assigned to a variable, the variable also has the value of `undefined`. To check for it, compare using the strict equality (`===`) operator or `typeof` which will give the `'undefined'` string. Note that you should not be using the loose equality operator (`==`) to check, as it will also return `true` if the value is `null`.
37+
A variable that is `undefined` is a variable that has been declared, but not assigned a value. It is of type `undefined`. If a function does not return a value, and its result is assigned to a variable, that variable will also have the value `undefined`. To check for it, compare using the strict equality (`===`) operator or `typeof` which will give the `'undefined'` string. Note that you should not be using the loose equality operator (`==`) to check, as it will also return `true` if the value is `null`.
3838

3939
```js live
4040
let foo;

‎questions/whats-the-difference-between-call-and-apply/en-US.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ const person1 = { name: 'John' };
8080
const person2 = { name: 'Alice' };
8181

8282
greet.call(person1); // Hello, my name is John
83-
greet.call(person2); // Hello, my name is Alice
83+
greet.apply(person2); // Hello, my name is Alice
8484
```
8585

8686
### Alternative syntax to call methods on objects

0 commit comments

Comments
 (0)
Please sign in to comment.