Skip to content

Commit cd6d7a7

Browse files
committed
feat: update technical accuracies
1 parent 918b5be commit cd6d7a7

61 files changed

Lines changed: 287 additions & 286 deletions

File tree

  • questions
    • describe-event-bubbling
    • describe-event-capturing
    • describe-the-difference-between-script-async-and-script-defer
    • difference-between-function-person-var-person-person-and-var-person-new-person
    • explain-ajax-in-as-much-detail-as-possible
    • explain-event-delegation
    • explain-function-prototype-bind
    • explain-hoisting
    • explain-how-jsonp-works-and-how-its-not-really-ajax
    • explain-how-prototypal-inheritance-works
    • explain-how-this-works-in-javascript
    • explain-the-concept-of-a-callback-function-in-asynchronous-operations
    • explain-the-concept-of-a-microtask-queue
    • explain-the-difference-between-mutable-and-immutable-objects
    • explain-the-difference-between-synchronous-and-asynchronous-functions
    • explain-the-differences-between-commonjs-modules-and-es-modules
    • explain-the-differences-on-the-usage-of-foo-between-function-foo-and-var-foo-function
    • how-do-you-abort-a-web-request-using-abortcontrollers
    • how-do-you-detect-if-javascript-is-disabled-on-a-page
    • how-do-you-make-an-http-request-using-the-fetch-api
    • how-do-you-validate-form-elements-using-the-constraint-validation-api
    • how-does-javascript-garbage-collection-work
    • what-advantage-is-there-for-using-the-arrow-syntax-for-a-method-in-a-constructor
    • what-are-iterators-and-generators-and-what-are-they-used-for
    • what-are-javascript-object-getters-and-setters-for
    • what-are-javascript-object-property-flags-and-descriptors
    • what-are-javascript-polyfills-for
    • what-are-proxies-in-javascript-used-for
    • what-are-server-sent-events
    • what-are-symbols-used-for
    • what-are-the-advantages-and-disadvantages-of-using-ajax
    • what-are-the-benefits-of-using-spread-syntax-and-how-is-it-different-from-rest-syntax
    • what-are-the-differences-between-es6-class-and-es5-function-constructors
    • what-are-the-differences-between-map-set-and-weakmap-weakset
    • what-are-the-differences-between-variables-created-using-let-var-or-const
    • what-are-the-differences-between-xmlhttprequest-and-fetch
    • what-are-the-pros-and-cons-of-using-promises-instead-of-callbacks
    • what-are-the-various-data-types-in-javascript
    • what-are-the-various-ways-to-create-objects-in-javascript
    • what-are-web-workers-and-how-can-they-be-used-to-improve-performance
    • what-are-workers-in-javascript-used-for
    • what-is-a-closure-and-how-why-would-you-use-one
    • what-is-event-loop-what-is-the-difference-between-call-stack-and-task-queue
    • what-is-the-command-pattern-and-how-is-it-used
    • what-is-the-definition-of-a-higher-order-function
    • what-is-the-difference-between-a-map-object-and-a-plain-object-in-javascript
    • what-is-the-difference-between-double-equal-and-triple-equal
    • what-is-the-difference-between-mouseenter-and-mouseover-event
    • what-is-the-purpose-of-the-finally-block
    • what-is-the-spread-operator-and-how-is-it-used
    • what-is-use-strict-what-are-the-advantages-and-disadvantages-to-using-it
    • what-language-constructs-do-you-use-for-iterating-over-object-properties-and-array-items
    • whats-a-typical-use-case-for-anonymous-functions
    • 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
    • why-is-extending-built-in-javascript-objects-not-a-good-idea
    • why-is-it-in-general-a-good-idea-to-leave-the-global-scope-of-a-website-as-is-and-never-touch-it
    • why-you-might-want-to-create-static-class-members

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 102 additions & 102 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
],
1010
"license": "ISC",
1111
"author": "yangshun",
12+
"type": "module",
1213
"main": "index.js",
1314
"scripts": {
1415
"gen": "tsx scripts/gen.ts",

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Describe event bubbling in JavaScript and browsers
44

55
## TL;DR
66

7-
Event bubbling is a DOM event propagation mechanism where an event (e.g. a click), starts at the target element and bubbles up to the root of the document. This allows ancestor elements to also respond to the event.
7+
Event bubbling is a DOM event propagation mechanism where an event (e.g. a click) starts at the target element and bubbles up to the root of the document. This allows ancestor elements to also respond to the event.
88

99
Event bubbling is essential for event delegation, where a single event handler manages events for multiple child elements, enhancing performance and code simplicity. While convenient, failing to manage event propagation properly can lead to unintended behavior, such as multiple handlers firing for a single event.
1010

@@ -14,7 +14,7 @@ Event bubbling is essential for event delegation, where a single event handler m
1414

1515
Event bubbling is a propagation mechanism in the DOM (Document Object Model) where an event, such as a click or a keyboard event, is first triggered on the target element that initiated the event and then propagates upward (bubbles) through the DOM tree to the root of the document.
1616

17-
**Note**: even before the event bubbling phase happens is the [event capturing](/questions/quiz/describe-event-capturing) phase which is the opposite of bubbling where the event goes down from the document root to the target element.
17+
**Note**: before the event bubbling phase, there is the [event capturing](/questions/quiz/describe-event-capturing) phase, which is the opposite of bubbling, where the event travels down from the document root to the target element.
1818

1919
## Bubbling phase
2020

@@ -49,7 +49,7 @@ child.addEventListener('click', () => {
4949
child.click();
5050
```
5151

52-
When you click the "Click me!" button, both the child and parent event handlers will be triggered due to the event bubbling.
52+
When you click the "Click me!" button, both the child and parent event handlers will be triggered due to event bubbling.
5353

5454
## Stopping the bubbling
5555

@@ -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/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.
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 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) => {
@@ -156,7 +156,7 @@ function handleBuyClick(event) {
156156
}
157157
```
158158

159-
By attaching the listener to the parent (`productList`) and checking the clicked element (`event.target`) within the handler, you achieve the same functionality with less code. This approach scales well when the items are dynamic as no new event handlers have to be added or removed when the list of items change.
159+
By attaching the listener to the parent (`productList`) and checking the clicked element (`event.target`) within the handler, you achieve the same functionality with less code. This approach scales well when the items are dynamic, as no new event handlers have to be added or removed when the list of items changes.
160160

161161
### Dropdown menus
162162

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ Capturing has a higher priority than bubbling, meaning that capturing event hand
2020
- **Target phase**: The event reaches the target element
2121
- **Bubbling phase**: The event bubbles up from the target element
2222

23-
Note that event capturing is disabled by default. To enable it you have to pass the capture option into `addEventListener()`.
23+
Note that event capturing is disabled by default. To enable it, you have to pass the capture option into `addEventListener()`.
2424

2525
## Capturing phase
2626

27-
During the capturing phase, the event starts at the document root and propagates down to the target element. Any event listeners on ancestor elements in this path will be triggered before the target element's handler. But note that event capturing can't happen until the third argument of `addEventListener()` is set to `true` as shown below (default value is `false`).
27+
During the capturing phase, the event starts at the document root and propagates down to the target element. Any event listeners on ancestor elements in this path will be triggered before the target element's handler. Note that event capturing will not happen unless the third argument of `addEventListener()` is set to `true` as shown below (the default value is `false`).
2828

2929
Here's an example using modern ES2015 syntax to demonstrate event capturing:
3030

@@ -79,15 +79,15 @@ child.addEventListener('click', () => {
7979
});
8080
```
8181

82-
As a result of stopping event propagation, just the `parent` event listener will now be called when you click the "Click Me!" button, and the `child` event listener will never be called because the event propagation has stopped at the `parent` element.
82+
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

8484
## Uses of event capturing
8585

8686
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.
8787

8888
- **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.
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.
9191

9292
## Further reading
9393

questions/describe-the-difference-between-a-cookie-sessionstorage-and-localstorage/en-US.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ These client-side storage mechanisms have the following common properties:
3838

3939
Since cookies have a relatively low maximum size, it is not advisable to store all your client-side data within cookies. The distinguishing properties about cookies are that cookies are sent to the server on every HTTP request so the low maximum size is a feature that prevents your HTTP requests from being too large due to cookies. Automatic expiry of cookies is a useful feature as well.
4040

41-
With that in mind, the best kind of data to store within cookies is small pieces of data that needs to be transmitted to the server, such as auth tokens, session IDs, analytics tracking IDs, GDPR cookie consent, language preferences that are important for authentication, authorization, and rendering on the server. These values are sometimes sensitive and can benefit from the `HttpOnly`, `Secure`, and `Expires`/`Max-Age` capabilities that cookies provide.
41+
With that in mind, the best kind of data to store within cookies is small pieces of data that need to be transmitted to the server, such as auth tokens, session IDs, analytics tracking IDs, GDPR cookie consent, language preferences that are important for authentication, authorization, and rendering on the server. These values are sometimes sensitive and can benefit from the `HttpOnly`, `Secure`, and `Expires`/`Max-Age` capabilities that cookies provide.
4242

43-
`localStorage` and `sessionStorage` both implement the [Web Storage API interface](https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API). Web Storages have a generous total capacity of 5MB, so storage size is usually not a concern. The key difference is that values stored in Web Storage are not automatically sent along HTTP requests.
43+
`localStorage` and `sessionStorage` both implement the [Web Storage API interface](https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API). Web Storage has a generous total capacity of 5MB, so storage size is usually not a concern. The key difference is that values stored in Web Storage are not automatically sent along with HTTP requests.
4444

4545
While you can manually include values from Web Storage when making AJAX/`fetch()` requests, the browser does not include them in the initial request / first load of the page. Hence Web Storage should not be used to store data that is relied on by the server for the initial rendering of the page if server-side rendering is being used (typically authentication/authorization-related information). `localStorage` is most suitable for user preferences data that do not expire, like themes and layouts (if it is not important for the server to render the final layout). `sessionStorage` is most suitable for temporary data that only needs to be accessible within the current browsing session, such as form data (useful to preserve data during accidental reloads).
4646

@@ -98,7 +98,7 @@ The CookieStore API is relatively new and may not be supported in all browsers (
9898
- **Storage capacity**: Typically around 5MB per origin (varies by browser).
9999
- **Lifespan**: Data in `localStorage` persists until explicitly deleted by the user or the application.
100100
- **Access**: Data is accessible within all tabs and windows of the same origin.
101-
- **Security**: All JavaScript on the page have access to values within `localStorage`.
101+
- **Security**: All JavaScript on the page has access to values within `localStorage`.
102102

103103
```js
104104
// Set a value in localStorage.
@@ -121,7 +121,7 @@ localStorage.clear();
121121
- **Storage Capacity**: Typically around 5MB per origin (varies by browser).
122122
- **Lifespan**: Data in `sessionStorage` is cleared when the page session ends (i.e., when the browser or tab is closed). Reloading the page does not destroy data within `sessionStorage`.
123123
- **Access**: Data is accessible only within the current tab (or browsing context). Different tabs share different `sessionStorage` objects even if they belong to the same browser window. In this context, window refers to a browser window that can contain multiple tabs.
124-
- **Security**: All JavaScript on the same page have access to values within `sessionStorage` for that page.
124+
- **Security**: All JavaScript on the same page has access to values within `sessionStorage` for that page.
125125

126126
```js
127127
// Set a value in sessionStorage.

questions/describe-the-difference-between-script-async-and-script-defer/en-US.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ title: Describe the difference between `<script>`, `<script async>` and `<script
77
All of these ways (`<script>`, `<script async>`, and `<script defer>`) are used to load and execute JavaScript files in an HTML document, but they differ in how the browser handles loading and execution of the script:
88

99
- `<script>` is the default way of including JavaScript. The browser blocks HTML parsing while the script is being downloaded and executed. The browser will not continue rendering the page until the script has finished executing.
10-
- `<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. `<script async>` do not wait for each other and execute in no particular order.
10+
- `<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.
1111
- `<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.
1212

13-
Here's a table summarizing the 3 ways of loading `<script>`s in a HTML document.
13+
Here's a table summarizing the 3 ways of loading `<script>`s in an HTML document.
1414

1515
| Feature | `<script>` | `<script async>` | `<script defer>` |
1616
| --- | --- | --- | --- |
@@ -82,7 +82,7 @@ Use `<script async>` when the script is independent of any other scripts on the
8282

8383
## `<script defer>`
8484

85-
Similar to `<script async>`, `<script defer>` also downloads the script in parallel to HTML parsing but the script is only executed when the document has been fully parsed and before firing `DOMContentLoaded`. If there are multiple of them, each deferred script is executed in the order they appeared in the HTML document.
85+
Similar to `<script async>`, `<script defer>` also downloads the script in parallel to HTML parsing, but the script is only executed when the document has been fully parsed and before firing `DOMContentLoaded`. If there are multiple of them, each deferred script is executed in the order they appear in the HTML document.
8686

8787
If a script relies on a fully-parsed DOM, the `defer` attribute will be useful in ensuring that the HTML is fully parsed before executing.
8888

@@ -108,7 +108,7 @@ If a script relies on a fully-parsed DOM, the `defer` attribute will be useful i
108108

109109
## Notes
110110

111-
- The `async` attribute should be used for scripts that are not critical to the initial rendering of the page and do not depend on each other, while the `defer` attribute should be used for scripts that depend on / is depended on by another script.
111+
- The `async` attribute should be used for scripts that are not critical to the initial rendering of the page and do not depend on each other, while the `defer` attribute should be used for scripts that depend on or are depended on by another script.
112112
- The `async` and `defer` attributes are ignored for scripts that have no `src` attribute.
113113
- `<script>`s with `defer` or `async` that contain `document.write()` will be ignored with a message like "A call to `document.write()` from an asynchronously-loaded external script was ignored".
114114
- Even though `async` and `defer` help to make script downloading asynchronous, the scripts are still eventually executed on the main thread. If these scripts are computationally intensive, it can result in laggy/frozen UI. [Partytown](https://partytown.qwik.dev/) is a library that helps relocate script executions into a [web worker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API) and off the [main thread](https://developer.mozilla.org/en-US/docs/Glossary/Main_thread), which is great for third-party scripts where you do not have control over the code.

questions/difference-between-function-person-var-person-person-and-var-person-new-person/en-US.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ This code defines a function named `Person` that takes a parameter `name` and as
3131

3232
## Function call
3333

34-
`const person = Person()` simply invoke the function's code. When you invoke `Person` as a regular function (i.e., without the `new` keyword), the function does not behave as a constructor. Instead, it executes its code and returns `undefined` if no return value is specified and that gets assigned to the variable intended as the instance. Invoking as such is a common mistake if the function is intended to be used as a constructor.
34+
`const person = Person()` simply invokes the function's code. When you invoke `Person` as a regular function (i.e., without the `new` keyword), the function does not behave as a constructor. Instead, it executes its code and returns `undefined` if no return value is specified, and that gets assigned to the variable intended as the instance. Invoking it this way is a common mistake if the function is intended to be used as a constructor.
3535

3636
```js live
3737
function Person(name) {
@@ -47,7 +47,7 @@ In this case, `Person('John')` does not create a new object. The `person` variab
4747

4848
## Constructor call
4949

50-
`const person = new Person()` creates an instance of the `Person` object using the new operator, which inherits from `Person.prototype`. An alternative would be to use `Object.create`, such as: `Object.create(Person.prototype)` and `Person.call(person, 'John')` initializes the object.
50+
`const person = new Person()` creates an instance of the `Person` object using the `new` operator, which inherits from `Person.prototype`. An alternative would be to use `Object.create`, such as: `Object.create(Person.prototype)` to create the object and `Person.call(person, 'John')` to initialize it.
5151

5252
```js live
5353
function Person(name) {
@@ -65,7 +65,7 @@ console.log(person1); // Person { name: 'John' }
6565
console.log(person1.name); // 'John'
6666
```
6767

68-
In this case, `new Person('John')` creates a new object, and `this` within `Person` refers to this new object. The `name` property is correctly set on the new object. The `person` variable is assigned the new instance of `Person` with the name property set to `'John'`. And for the alternative object creation, `Object.create(Person.prototype)` creates a new object with `Person.prototype` as its prototype. `Person.call(person, 'John')` initializes the object, setting the `name` property.
68+
In this case, `new Person('John')` creates a new object, and `this` within `Person` refers to this new object. The `name` property is correctly set on the new object. The `person` variable is assigned the new instance of `Person` with the `name` property set to `'John'`. For the alternative object creation, `Object.create(Person.prototype)` creates a new object with `Person.prototype` as its prototype, and `Person.call(person, 'John')` initializes the object, setting the `name` property.
6969

7070
## Follow-Up Questions
7171

0 commit comments

Comments
 (0)