Skip to content

Commit cf075fe

Browse files
feat: expand quiz answers based on the SEO action plan (#52)
Co-authored-by: GitHub Actions <github-actions[bot]@users.noreply.github.com>
1 parent 26e4057 commit cf075fe

13 files changed

Lines changed: 1070 additions & 60 deletions

File tree

  • questions
    • describe-event-capturing
    • describe-the-difference-between-script-async-and-script-defer
    • explain-event-delegation
    • explain-the-concept-of-debouncing-and-throttling
    • explain-the-difference-between-documentqueryselector-and-documentgetelementbyid
    • how-is-promiseall-different-from-promiseallsettled
    • what-are-server-sent-events
    • what-are-some-of-the-advantages-disadvantages-of-writing-javascript-code-in-a-language-that-compiles-to-javascript
    • what-are-the-advantages-and-disadvantages-of-using-ajax
    • what-is-use-strict-what-are-the-advantages-and-disadvantages-to-using-it
    • whats-the-difference-between-an-attribute-and-a-property

README.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ This list contains the top essential questions that are frequently-asked during
5353
| 21 | [Describe event bubbling in JavaScript and browsers](#describe-event-bubbling-in-javascript-and-browsers) | Basic |
5454
| 22 | [Describe event capturing in JavaScript and browsers](#describe-event-capturing-in-javascript-and-browsers) | Basic |
5555
| 23 | [What is the difference between `mouseenter` and `mouseover` event in JavaScript and browsers?](#what-is-the-difference-between-mouseenter-and-mouseover-event-in-javascript-and-browsers) | Basic |
56-
| 24 | [What is `'use strict';` in JavaScript for?](#what-is-use-strict-in-javascript-for) | Advanced |
56+
| 24 | [What is `'use strict';` (strict mode) in JavaScript for?](#what-is-use-strict-strict-mode-in-javascript-for) | Advanced |
5757
| 25 | [Explain the difference between synchronous and asynchronous functions in JavaScript](#explain-the-difference-between-synchronous-and-asynchronous-functions-in-javascript) | Basic |
5858
| 26 | [What are the pros and cons of using Promises instead of callbacks in JavaScript?](#what-are-the-pros-and-cons-of-using-promises-instead-of-callbacks-in-javascript) | Intermediate |
5959
| 27 | [Explain AJAX in as much detail as possible](#explain-ajax-in-as-much-detail-as-possible) | Basic |
@@ -276,13 +276,13 @@ This list contains a longer list of important JavaScript questions. Not all of t
276276
| 183 | [What are some tools and techniques for identifying security vulnerabilities in JavaScript code?](#what-are-some-tools-and-techniques-for-identifying-security-vulnerabilities-in-javascript-code) | Intermediate |
277277
| 184 | [How can you implement secure authentication and authorization in JavaScript applications?](#how-can-you-implement-secure-authentication-and-authorization-in-javascript-applications) | Advanced |
278278
| 185 | [Explain the same-origin policy with regards to JavaScript](#explain-the-same-origin-policy-with-regards-to-javascript) | Intermediate |
279-
| 186 | [What is `'use strict';` in JavaScript for?](#what-is-use-strict-in-javascript-for) | Advanced |
279+
| 186 | [What is `'use strict';` (strict mode) in JavaScript for?](#what-is-use-strict-strict-mode-in-javascript-for) | Advanced |
280280
| 187 | [What tools and techniques do you use for debugging JavaScript code?](#what-tools-and-techniques-do-you-use-for-debugging-javascript-code) | Intermediate |
281281
| 188 | [How does JavaScript garbage collection work?](#how-does-javascript-garbage-collection-work) | Advanced |
282282
| 189 | [Explain what a single page app is and how to make one SEO-friendly](#explain-what-a-single-page-app-is-and-how-to-make-one-seo-friendly) | Intermediate |
283283
| 190 | [How can you share code between JavaScript files?](#how-can-you-share-code-between-javascript-files) | Basic |
284284
| 191 | [How do you organize your code?](#how-do-you-organize-your-code) | Intermediate |
285-
| 192 | [What are some of the advantages/disadvantages of writing JavaScript code in a language that compiles to JavaScript?](#what-are-some-of-the-advantagesdisadvantages-of-writing-javascript-code-in-a-language-that-compiles-to-javascript) | Advanced |
285+
| 192 | [What are some of the advantages and disadvantages of using TypeScript and compile-to-JavaScript languages](#what-are-some-of-the-advantages-and-disadvantages-of-using-typescript-and-compile-to-javascript-languages) | Advanced |
286286
| 193 | [When would you use `document.write()`?](#when-would-you-use-documentwrite) | Advanced |
287287

288288
<!-- TABLE_OF_CONTENTS:ALL:END -->
@@ -497,13 +497,13 @@ All of these ways (`<script>`, `<script async>`, and `<script defer>`) are used
497497
- `<script async>` downloads the script asynchronously, in parallel with parsing the HTML. Executes the script as soon as it is available, potentially interrupting the HTML parsing. Multiple `<script async>` tags do not wait for each other and execute in no particular order.
498498
- `<script defer>` downloads the script asynchronously, in parallel with parsing the HTML. However, the execution of the script is deferred until HTML parsing is complete, in the order they appear in the HTML.
499499

500-
Here's a table summarizing the 3 ways of loading `<script>`s in an HTML document.
500+
Here's a table summarizing the 4 ways of loading `<script>`s in an HTML document. Modern apps almost always use modules, which deserve their own row.
501501

502-
| Feature | `<script>` | `<script async>` | `<script defer>` |
503-
| --- | --- | --- | --- |
504-
| Parsing behavior | Blocks HTML parsing | Runs parallel to parsing | Runs parallel to parsing |
505-
| Execution order | In order of appearance | Not guaranteed | In order of appearance |
506-
| DOM dependency | No | No | Yes (waits for DOM) |
502+
| Feature | `<script>` | `<script async>` | `<script defer>` | `<script type="module">` |
503+
| --- | --- | --- | --- | --- |
504+
| Parsing behavior | Blocks HTML parsing | Downloads in parallel; execution still blocks parsing | Downloads in parallel; execution deferred until after parsing | Downloads in parallel; execution deferred until after parsing |
505+
| Execution order | In order of appearance | Not guaranteed | In order of appearance | In order of appearance, with each script's `import` dependencies resolved first |
506+
| DOM dependency | No | No | Yes (waits for DOM) | Yes (waits for DOM) |
507507

508508
<!-- Update here: /questions/describe-the-difference-between-script-async-and-script-defer/en-US.mdx -->
509509

@@ -1077,7 +1077,7 @@ The main difference lies in the bubbling behavior of `mouseenter` and `mouseover
10771077

10781078
<br>
10791079

1080-
### What is `'use strict';` in JavaScript for?
1080+
### What is `'use strict';` (strict mode) in JavaScript for?
10811081

10821082
<!-- Update here: /questions/what-is-use-strict-what-are-the-advantages-and-disadvantages-to-using-it/en-US.mdx -->
10831083

@@ -4982,13 +4982,13 @@ All of these ways (`<script>`, `<script async>`, and `<script defer>`) are used
49824982
- `<script async>` downloads the script asynchronously, in parallel with parsing the HTML. Executes the script as soon as it is available, potentially interrupting the HTML parsing. Multiple `<script async>` tags do not wait for each other and execute in no particular order.
49834983
- `<script defer>` downloads the script asynchronously, in parallel with parsing the HTML. However, the execution of the script is deferred until HTML parsing is complete, in the order they appear in the HTML.
49844984

4985-
Here's a table summarizing the 3 ways of loading `<script>`s in an HTML document.
4985+
Here's a table summarizing the 4 ways of loading `<script>`s in an HTML document. Modern apps almost always use modules, which deserve their own row.
49864986

4987-
| Feature | `<script>` | `<script async>` | `<script defer>` |
4988-
| --- | --- | --- | --- |
4989-
| Parsing behavior | Blocks HTML parsing | Runs parallel to parsing | Runs parallel to parsing |
4990-
| Execution order | In order of appearance | Not guaranteed | In order of appearance |
4991-
| DOM dependency | No | No | Yes (waits for DOM) |
4987+
| Feature | `<script>` | `<script async>` | `<script defer>` | `<script type="module">` |
4988+
| --- | --- | --- | --- | --- |
4989+
| Parsing behavior | Blocks HTML parsing | Downloads in parallel; execution still blocks parsing | Downloads in parallel; execution deferred until after parsing | Downloads in parallel; execution deferred until after parsing |
4990+
| Execution order | In order of appearance | Not guaranteed | In order of appearance | In order of appearance, with each script's `import` dependencies resolved first |
4991+
| DOM dependency | No | No | Yes (waits for DOM) | Yes (waits for DOM) |
49924992

49934993
<!-- Update here: /questions/describe-the-difference-between-script-async-and-script-defer/en-US.mdx -->
49944994

@@ -6979,7 +6979,7 @@ The same-origin policy is a security measure implemented in web browsers to prev
69796979

69806980
<br>
69816981

6982-
### What is `'use strict';` in JavaScript for?
6982+
### What is `'use strict';` (strict mode) in JavaScript for?
69836983

69846984
<!-- Update here: /questions/what-is-use-strict-what-are-the-advantages-and-disadvantages-to-using-it/en-US.mdx -->
69856985

@@ -7140,11 +7140,11 @@ I organize my code by following a modular approach, using a clear folder structu
71407140

71417141
<br>
71427142

7143-
### What are some of the advantages/disadvantages of writing JavaScript code in a language that compiles to JavaScript?
7143+
### What are some of the advantages and disadvantages of using TypeScript and compile-to-JavaScript languages
71447144

71457145
<!-- Update here: /questions/what-are-some-of-the-advantages-disadvantages-of-writing-javascript-code-in-a-language-that-compiles-to-javascript/en-US.mdx -->
71467146

7147-
Using languages that compile to JavaScript, like TypeScript or CoffeeScript, can offer several advantages such as improved syntax, type safety, and better tooling. However, they also come with disadvantages like added build steps, potential performance overhead, and the need to learn new syntax.
7147+
Using languages that compile to JavaScript (most commonly TypeScript today, but historically also CoffeeScript, ReScript, Elm, and ClojureScript) can offer several advantages such as improved syntax, type safety, and better tooling. However, they also come with disadvantages like added build steps, potential performance overhead, and the need to learn new syntax.
71487148

71497149
Advantages:
71507150

@@ -7377,7 +7377,7 @@ JavaScript interview questions categorized by difficulty.
73777377

73787378
<!-- QUESTIONS:ADVANCED:START -->
73797379

7380-
1. [What is `'use strict';` in JavaScript for?](#what-is-use-strict-in-javascript-for)
7380+
1. [What is `'use strict';` (strict mode) in JavaScript for?](#what-is-use-strict-strict-mode-in-javascript-for)
73817381
2. [What are JavaScript polyfills for?](#what-are-javascript-polyfills-for)
73827382
3. [What are iterators and generators in JavaScript and what are they used for?](#what-are-iterators-and-generators-in-javascript-and-what-are-they-used-for)
73837383
4. [What are server-sent events?](#what-are-server-sent-events)
@@ -7393,7 +7393,7 @@ JavaScript interview questions categorized by difficulty.
73937393
14. [How do you validate form elements using the Constraint Validation API?](#how-do-you-validate-form-elements-using-the-constraint-validation-api)
73947394
15. [How does hoisting affect function declarations and expressions?](#how-does-hoisting-affect-function-declarations-and-expressions)
73957395
16. [What are mocks and stubs and how are they used in testing?](#what-are-mocks-and-stubs-and-how-are-they-used-in-testing)
7396-
17. [What are some of the advantages/disadvantages of writing JavaScript code in a language that compiles to JavaScript?](#what-are-some-of-the-advantagesdisadvantages-of-writing-javascript-code-in-a-language-that-compiles-to-javascript)
7396+
17. [What are some of the advantages and disadvantages of using TypeScript and compile-to-JavaScript languages](#what-are-some-of-the-advantages-and-disadvantages-of-using-typescript-and-compile-to-javascript-languages)
73977397
18. [What are some techniques for reducing reflows and repaints?](#what-are-some-techniques-for-reducing-reflows-and-repaints)
73987398
19. [What are some tools that can be used to measure and analyze JavaScript performance?](#what-are-some-tools-that-can-be-used-to-measure-and-analyze-javascript-performance)
73997399
20. [What are Web Workers and how can they be used to improve performance?](#what-are-web-workers-and-how-can-they-be-used-to-improve-performance)

questions/describe-event-capturing/en-US.mdx

Lines changed: 97 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,105 @@ child.addEventListener('click', () => {
8181

8282
As a result of stopping event propagation, only the `parent` event listener will be called when you click the "Click me!" button, and the `child` event listener will never be called because event propagation has stopped at the `parent` element.
8383

84-
## Uses of event capturing
84+
## Predict the output: capture, target, bubble in order
8585

86-
Event capturing is rarely used as compared to event bubbling, but it can be used in specific scenarios where you need to intercept events at a higher level before they reach the target element.
86+
The complete event flow is the part candidates most often get wrong. The same `click` event passes through every ancestor on the way down (capture), arrives at the target, then walks back up (bubble). Here is the full sequence in one runnable example:
8787

88-
- **Stopping event bubbling:** Imagine you have a nested element (like a button) inside a container element. Clicking the button might also trigger a click event on the container. By enabling event capturing on the container's event listener, you can capture the click event there and prevent it from traveling down to the button, potentially causing unintended behavior.
89-
- **Custom dropdown menus:** When building custom dropdown menus, you might want to capture clicks outside the menu element to close the menu. Using `capture: true` on the `document` object allows you to listen for clicks anywhere on the page and close the menu if the click happens outside its boundaries.
90-
- **Efficiency in certain scenarios:** In some situations, event capturing can be slightly more efficient than relying on bubbling. This is because the event doesn't need to propagate through all child elements before reaching the handler. However, the performance difference is usually negligible for most web applications.
88+
```js live
89+
const grandparent = document.createElement('div');
90+
const parent = document.createElement('div');
91+
const child = document.createElement('button');
92+
grandparent.appendChild(parent);
93+
parent.appendChild(child);
94+
document.body.appendChild(grandparent);
95+
96+
// Capture handlers (third arg = true)
97+
grandparent.addEventListener(
98+
'click',
99+
() => console.log('1. grandparent capture'),
100+
true,
101+
);
102+
parent.addEventListener('click', () => console.log('2. parent capture'), true);
103+
104+
// Target handler (default: bubble phase)
105+
child.addEventListener('click', () => console.log('3. target'));
106+
107+
// Bubble handlers
108+
parent.addEventListener('click', () => console.log('4. parent bubble'));
109+
grandparent.addEventListener('click', () =>
110+
console.log('5. grandparent bubble'),
111+
);
112+
113+
child.click();
114+
// Output (in this exact order):
115+
// 1. grandparent capture
116+
// 2. parent capture
117+
// 3. target
118+
// 4. parent bubble
119+
// 5. grandparent bubble
120+
```
121+
122+
The full picture: events go down, then up. Capture handlers fire from the root toward the target; bubble handlers fire from the target back toward the root. The target's own handler runs in the middle. (Listeners on the target itself fire in registration order regardless of the `capture` argument; the capture/bubble distinction only applies to ancestors.)
123+
124+
## Bubbling vs capturing comparison
125+
126+
| | Capturing | Bubbling |
127+
| --- | --- | --- |
128+
| Phase order | First (down from root) | Last (up to root) |
129+
| `useCapture` argument | `true` (or `{ capture: true }`) | `false` (the default) |
130+
| Default behavior | Off; must opt in | On; every listener bubbles by default |
131+
| Effect of `event.stopPropagation()` | Stops the event before it reaches the target | Stops the event before higher ancestors see it |
132+
| Common use cases | Intercepting non-bubbling events; pre-empting child handlers; analytics | Most click, input, and change handlers; event delegation |
133+
134+
## When to use the capture phase in real apps
135+
136+
In practice, the capture phase is the right tool for three specific situations:
137+
138+
1. **Delegating non-bubbling events.** `focus`, `blur`, `scroll`, and `mouseenter`/`mouseleave` do not bubble, but they are visible to ancestors during the capture phase. Adding `addEventListener('focus', handler, true)` to a form gives you a delegated focus listener for every input inside it.
139+
140+
```js
141+
form.addEventListener(
142+
'focus',
143+
(event) => {
144+
highlightField(event.target);
145+
},
146+
true, // capture: catches focus events before they stop at the input
147+
);
148+
```
149+
150+
2. **Pre-empting child handlers** for analytics or feature gates. The capture phase runs before any child's bubble handler, so a region-wide "intercept clicks" listener can record the click (or block the action with `stopPropagation()`) before component code sees it.
151+
152+
3. **Modal libraries that need first-look at clicks.** A modal dialog often listens at the document level with `capture: true` for outside-click dismissal. Using the bubble phase would let inner handlers call `stopPropagation()` and accidentally prevent the modal from closing.
153+
154+
## `stopPropagation()` in capture vs bubble
155+
156+
`stopPropagation()` blocks all subsequent phases, not just the next ancestor.
157+
158+
```js live
159+
const outer = document.createElement('div');
160+
const inner = document.createElement('button');
161+
outer.appendChild(inner);
162+
document.body.appendChild(outer);
163+
164+
outer.addEventListener(
165+
'click',
166+
(event) => {
167+
console.log('outer capture: stopping here');
168+
event.stopPropagation();
169+
},
170+
true,
171+
);
172+
inner.addEventListener('click', () => console.log('inner target'));
173+
outer.addEventListener('click', () => console.log('outer bubble'));
174+
175+
inner.click();
176+
// Output: 'outer capture: stopping here'
177+
// The target handler and bubble handler are both skipped.
178+
```
179+
180+
Calling `stopPropagation()` during the capture phase prevents the target's own handlers and every bubble-phase ancestor from running. This is useful for an "intercept and replace" pattern, but if the target needs to keep working, listen during the bubble phase instead.
181+
182+
There is also `event.stopImmediatePropagation()`, which additionally prevents other listeners on the same element (registered in the same phase) from firing. Use it when multiple scripts add listeners to the same element and only one of them should run.
91183

92184
## Further reading
93185

0 commit comments

Comments
 (0)