You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+21-21Lines changed: 21 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -53,7 +53,7 @@ This list contains the top essential questions that are frequently-asked during
53
53
| 21 |[Describe event bubbling in JavaScript and browsers](#describe-event-bubbling-in-javascript-and-browsers)| Basic |
54
54
| 22 |[Describe event capturing in JavaScript and browsers](#describe-event-capturing-in-javascript-and-browsers)| Basic |
55
55
| 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 |
57
57
| 25 |[Explain the difference between synchronous and asynchronous functions in JavaScript](#explain-the-difference-between-synchronous-and-asynchronous-functions-in-javascript)| Basic |
58
58
| 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 |
59
59
| 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
276
276
| 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 |
277
277
| 184 |[How can you implement secure authentication and authorization in JavaScript applications?](#how-can-you-implement-secure-authentication-and-authorization-in-javascript-applications)| Advanced |
278
278
| 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 |
280
280
| 187 |[What tools and techniques do you use for debugging JavaScript code?](#what-tools-and-techniques-do-you-use-for-debugging-javascript-code)| Intermediate |
281
281
| 188 |[How does JavaScript garbage collection work?](#how-does-javascript-garbage-collection-work)| Advanced |
282
282
| 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 |
283
283
| 190 |[How can you share code between JavaScript files?](#how-can-you-share-code-between-javascript-files)| Basic |
284
284
| 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 |
286
286
| 193 |[When would you use `document.write()`?](#when-would-you-use-documentwrite)| Advanced |
287
287
288
288
<!-- TABLE_OF_CONTENTS:ALL:END -->
@@ -497,13 +497,13 @@ All of these ways (`<script>`, `<script async>`, and `<script defer>`) are used
497
497
-`<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.
498
498
-`<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.
499
499
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.
| 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) |
@@ -4982,13 +4982,13 @@ All of these ways (`<script>`, `<script async>`, and `<script defer>`) are used
4982
4982
-`<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.
4983
4983
-`<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.
4984
4984
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.
| 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) |
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.
7148
7148
7149
7149
Advantages:
7150
7150
@@ -7377,7 +7377,7 @@ JavaScript interview questions categorized by difficulty.
7377
7377
7378
7378
<!-- QUESTIONS:ADVANCED:START -->
7379
7379
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)
7381
7381
2.[What are JavaScript polyfills for?](#what-are-javascript-polyfills-for)
7382
7382
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)
7383
7383
4.[What are server-sent events?](#what-are-server-sent-events)
@@ -7393,7 +7393,7 @@ JavaScript interview questions categorized by difficulty.
7393
7393
14.[How do you validate form elements using the Constraint Validation API?](#how-do-you-validate-form-elements-using-the-constraint-validation-api)
7394
7394
15.[How does hoisting affect function declarations and expressions?](#how-does-hoisting-affect-function-declarations-and-expressions)
7395
7395
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)
7397
7397
18.[What are some techniques for reducing reflows and repaints?](#what-are-some-techniques-for-reducing-reflows-and-repaints)
7398
7398
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)
7399
7399
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)
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.
83
83
84
-
## Uses of event capturing
84
+
## Predict the output: capture, target, bubble in order
85
85
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:
87
87
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.
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) |
| 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.
// 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.
0 commit comments