Skip to content

qns: address user feedback #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ This list contains a longer list of important JavaScript questions. Not all of t
| 127 | [What are the benefits of using a module bundler?](#what-are-the-benefits-of-using-a-module-bundler) | Intermediate |
| 128 | [Explain the concept of tree shaking in module bundling](#explain-the-concept-of-tree-shaking-in-module-bundling) | Intermediate |
| 129 | [What are the metadata fields of a module?](#what-are-the-metadata-fields-of-a-module) | Intermediate |
| 130 | [What do you think of AMD vs CommonJS?](#what-do-you-think-of-amd-vs-commonjs) | Basic |
| 130 | [What do you think of CommonJS vs ESM?](#what-do-you-think-of-commonjs-vs-esm) | Basic |
| 131 | [What are the different types of errors in JavaScript?](#what-are-the-different-types-of-errors-in-javascript) | Intermediate |
| 132 | [How do you handle errors using `try...catch` blocks?](#how-do-you-handle-errors-using-trycatch-blocks) | Basic |
| 133 | [What is the purpose of the `finally` block?](#what-is-the-purpose-of-the-finally-block) | Basic |
Expand Down Expand Up @@ -5776,11 +5776,11 @@ Metadata fields of a module typically include information such as the module's n

<br>

### What do you think of AMD vs CommonJS?
### What do you think of CommonJS vs ESM?

<!-- Update here: /questions/what-do-you-think-of-amd-vs-commonjs/en-US.mdx -->

AMD (Asynchronous Module Definition) and CommonJS are two JavaScript module systems. AMD is designed for asynchronous loading of modules, making it suitable for browsers. CommonJS is designed for synchronous loading, making it more suitable for server-side environments like Node.js. AMD uses `define` and `require` for defining and loading modules, while CommonJS uses `module.exports` and `require`.
JavaScript has evolved its module systems. ESM (ECMAScript Modules) using `import` / `export` is the official standard, natively supported in modern browsers and Node.js, designed for both synchronous and asynchronous use cases. CommonJS (CJS) using `require` / `module.exports` was the original standard for Node.js, primarily synchronous, and remains prevalent in the Node ecosystem. AMD (Asynchronous Module Definition) using `define` / `require` was an early system designed for asynchronous loading in browsers but is now largely obsolete, replaced by ESM.

<!-- Update here: /questions/what-do-you-think-of-amd-vs-commonjs/en-US.mdx -->

Expand Down Expand Up @@ -7290,7 +7290,7 @@ JavaScript interview questions categorized by difficulty.
87. [What are the different methods for iterating over an array?](#what-are-the-different-methods-for-iterating-over-an-array)
88. [What are the different ways to copy an object or an array?](#what-are-the-different-ways-to-copy-an-object-or-an-array)
89. [What are the different ways to make an API call in JavaScript?](#what-are-the-different-ways-to-make-an-api-call-in-javascript)
90. [What do you think of AMD vs CommonJS?](#what-do-you-think-of-amd-vs-commonjs)
90. [What do you think of CommonJS vs ESM?](#what-do-you-think-of-commonjs-vs-esm)
91. [What is recursion and how is it used in JavaScript?](#what-is-recursion-and-how-is-it-used-in-javascript)
92. [What is the difference between a parameter and an argument?](#what-is-the-difference-between-a-parameter-and-an-argument)
93. [What is the DOM and how is it structured?](#what-is-the-dom-and-how-is-it-structured)
Expand Down
6 changes: 3 additions & 3 deletions questions/explain-how-prototypal-inheritance-works/en-US.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ function Dog(name, breed) {
this.breed = breed;
}

// Set the prototype of Dog to be a new instance of Animal
Dog.prototype = Object.create(Animal.prototype);
// Set the prototype of Dog to inherit from Animal's prototype
Object.setPrototypeOf(Dog.prototype, Animal.prototype);

// Add a method to the Dog prototype
Dog.prototype.bark = function () {
Expand All @@ -140,7 +140,7 @@ fido.sayName(); // "My name is Fido"
console.log(fido.fly); // undefined
```

4. **`Object.create()`**: This method creates a new object with the specified prototype object and properties. It's a straightforward way to set up prototypical inheritance. If you create a object via `Object.create(null)` it will not inherit any properties from `Object.prototype`. This means the object will not have any built-in properties or methods like `toString()`, `hasOwnProperty()`,
4. **`Object.create()`**: This method creates a new object whose internal `[[Prototype]]` is directly linked to the specified prototype object. It's the most direct way to create an object that inherits from another specific object, without involving constructor functions. If you create a object via `Object.create(null)` it will not inherit any properties from `Object.prototype`. This means the object will not have any built-in properties or methods like `toString()`, `hasOwnProperty()`,

```js live
// Define a prototype object
Expand Down
98 changes: 55 additions & 43 deletions questions/what-do-you-think-of-amd-vs-commonjs/en-US.mdx
Original file line number Diff line number Diff line change
@@ -1,42 +1,16 @@
---
title: What do you think of AMD vs CommonJS?
title: What do you think of CommonJS vs ESM?
---

## TL;DR

AMD (Asynchronous Module Definition) and CommonJS are two JavaScript module systems. AMD is designed for asynchronous loading of modules, making it suitable for browsers. CommonJS is designed for synchronous loading, making it more suitable for server-side environments like Node.js. AMD uses `define` and `require` for defining and loading modules, while CommonJS uses `module.exports` and `require`.
JavaScript has evolved its module systems. ESM (ECMAScript Modules) using `import` / `export` is the official standard, natively supported in modern browsers and Node.js, designed for both synchronous and asynchronous use cases. CommonJS (CJS) using `require` / `module.exports` was the original standard for Node.js, primarily synchronous, and remains prevalent in the Node ecosystem. AMD (Asynchronous Module Definition) using `define` / `require` was an early system designed for asynchronous loading in browsers but is now largely obsolete, replaced by ESM.

---

## AMD vs CommonJS

### Overview

Both AMD and CommonJS are module systems used in JavaScript to manage dependencies and organize code. They serve different purposes and are suited to different environments.

### AMD (Asynchronous Module Definition)

#### Characteristics

- Designed for asynchronous loading of modules
- Primarily used in browser environments
- Uses `define` and `require` for defining and loading modules

#### Example

```javascript
// Defining a module in AMD
define(['dependency1', 'dependency2'], function (dep1, dep2) {
return {
// Module code here
};
});

// Loading a module in AMD
require(['module1', 'module2'], function (mod1, mod2) {
// Code that uses mod1 and mod2
});
```
Module systems help manage this complexity by allowing code to be split into reusable pieces (modules) with clear dependencies. Over time, different module systems emerged before an official standard was adopted.

### CommonJS

Expand Down Expand Up @@ -64,37 +38,75 @@ const mod2 = require('module2');
// Code that uses mod1 and mod2
```

### ESM (ECMAScript Modules)

#### Characteristics

- Official JavaScript standard module system
- Supported natively in modern browsers and Node.js
- Designed for both asynchronous and synchronous scenarios
- Uses `export` and `import` for defining and loading modules

#### Example

```javascript
import dep1 from 'dependency1'; // if dependency1 had a default export
import { dep2 } from 'dependency2'; // named import to import something specific from dependency2

// Module code using dep1, dep2...

export function someFunction() {
// ... function logic ...
}
export const someValue = 'hello';

// Or export multiple things at once
// export { someFunction, someValue };

// Or export a default value
// export default class MyClass {
// ...
// }
```

### Key differences

#### Loading mechanism

- **AMD**: Asynchronous, suitable for browsers where non-blocking operations are crucial.
- **CommonJS**: Synchronous, suitable for server-side environments where modules are loaded before execution.
- **CommonJS**: Synchronous (blocks until loaded).
- **ESM**: Asynchronous-friendly (non-blocking in browsers).

#### Environment suitability

- **AMD**: Better for client-side applications.
- **CommonJS**: Better for server-side applications.
- **CommonJS**: Node.js legacy standard.
- **ESM**: Official standard (Browser & Node.js).

#### Syntax

- **AMD**: Uses `define` and `require`.
- **CommonJS**: Uses `module.exports` and `require`.
- **CommonJS**: `require()` / `module.exports`.
- **ESM**: `import` / `export`.

### Use cases
#### Analysis

#### AMD
- **CommonJS**: Dynamic (runtime analysis).
- **ESM**: Static (compile-time analysis, enables tree-shaking).

- Suitable for web applications where you need to load modules asynchronously to avoid blocking the UI.
- Often used with module loaders like RequireJS.
### Use cases

#### CommonJS

- Suitable for server-side applications where modules are loaded once at the start.
- The standard module system for Node.js.
- Older Node.js projects or where sync loading is needed.
- Default in Node.js unless configured for ESM.

#### ESM

- Modern web development (native browser support).
- New Node.js projects, especially those needing async features or optimizations.
- Code intended for both browser and server.

## Further reading

- [RequireJS documentation](https://requirejs.org/docs/api.html)
- [Node.js modules documentation](https://nodejs.org/api/modules.html)
- [MDN Web Docs on JavaScript modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules)
- [Node.js: CommonJS modules](https://nodejs.org/api/modules.html)
- [Node.js: ESM](https://nodejs.org/api/esm.html)
- [RequireJS (AMD Loader - Historical)](https://requirejs.org/docs/api.html)