Fix the table squishing issue

Created Diff never expires
16 removals
Lines
Total
Removed
Words
Total
Removed
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
10 lines
17 additions
Lines
Total
Added
Words
Total
Added
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
10 lines
<!-- language-all: javascript -->
<!-- language-all: javascript -->
| | `import` (ES Modules syntax) | `require` (CommonJS Modules syntax) |
| | `import` (ES Modules syntax) | `require` (CommonJS Modules syntax) |
|:---:|:---|:---|
|:---:|:---|:---|
| **Summary** | <p>It is the latest standard for working with modules in JavaScript and is supported in modern browsers and environments that transpile or support ES6, like TypeScript or Babel.</p> | <p>It was not originally part of JavaScript, but was adopted as the standard for Node.js, which has been routinely used in JavaScript server-side development.<br><br>While Node.js historically used CommonJS, it now also supports ES6 modules.</p> |
| **Summary** | <p>It is the latest standard for working with modules in JavaScript and is supported in modern browsers and environments that transpile or support ES6, like TypeScript or Babel.</p> | <p>It was not originally part of JavaScript, but was adopted as the standard for Node.js, which has been routinely used in JavaScript server-side development.<br><br>While Node.js historically used CommonJS, it now also supports ES6 modules.</p> |
| **Exports** | <div><p>Static (pre-defined). The structure of the module's exports is determined when the code is parsed, not while running.</p><p>This static nature allows tooling such as bundlers and linters to analyze the code without executing it, enabling features like better tree-shaking and faster load times in browsers.</p> For example: <pre><code>// myModule.js&#13;export function myFunc() { /\*...\*/ }&#13;export const MY_CONST = 123;</code></pre></div> | <div><p>Computed during runtime. The exports in a module are determined during the execution of the code.</p>For example: <pre><code>// myModule.js&#13;if (process.env.NODE_ENV === 'development') {&#13; module.exports.debug = function debug() {&#13; console.log('Debugging...');&#13; }&#13;} else {&#13; module.exports.log = function log() {&#13; console.log('Logging...');&#13; }&#13;}</code></pre></div> |
| **Exports** | <p>Static (pre-defined). The structure of the module's exports is determined when the code is parsed, not while running.</p><p>This static nature allows tooling such as bundlers and linters to analyze the code without executing it, enabling features like better tree-shaking and faster load times in browsers.</p> For example: <pre><code>// myModule.js&#13;export function myFunc() { /\*...\*/ }&#13;export const MY_CONST = 123;</code></pre> | &#13;<p>Computed during runtime. The exports in a module are determined during the execution of the code.</p>For example: <pre><code>// myModule.js&#13;if (process.env.NODE_ENV === 'development') {&#13; module.exports.debug = function debug() {&#13; console.log('Debugging...');&#13; }&#13;} else {&#13; module.exports.log = function log() {&#13; console.log('Logging...');&#13; }&#13;}</code></pre> |
| **Loading Modules** | <div><p>Can be asynchronous, allowing efficient, selective loading of module parts. This can result in faster load times and better performance.</p>For example: <pre><code>import { myFunc } from './myModule.js';&#13;&#13;myFunc();</code></pre></div> | <div><p>Synchronous (loads modules one by one). Always loads entire module, which could affect performance if the module is large.</p>For example: <pre><code>const { debug, log } = require('./myModule.js');&#13;&#13;if(debug) debug();&#13;if(log) log();</code></pre></div> |
| **Loading Modules** | &#13;<p>Can be asynchronous, allowing efficient, selective loading of module parts. This can result in faster load times and better performance.</p>For example: <pre><code>import { myFunc } from './myModule.js';&#13;&#13;myFunc();</code></pre> | &#13;<p>Synchronous (loads modules one by one). Always loads entire module, which could affect performance if the module is large.</p>For example: <pre><code>const { debug, log } = require('./myModule.js');&#13;&#13;if(debug) debug();&#13;if(log) log();</code></pre> |
| **Full Example** | <div>Make sure to export the function first. <pre><code>// somefile.js&#13;export function sayHello() {&#13; console.log("Hello, world!");&#13;}&#13;&#13;console.log("somefile has been loaded!");</code></pre> Then import it <pre><code>// main.js&#13;import { sayHello } from './somefile.js';&#13;&#13;sayHello();&#13;&#13;// 👇 Outputs 👇&#13;// "somefile has been loaded!"&#13;// "Hello, world!"</code></pre></div> | <div>Make sure to add the function to `module.exports`. <pre><code>// somefile.js&#13;function sayHello() {&#13; console.log("Hello, world!");&#13;}&#13;&#13;module.exports = { sayHello };&#13;&#13;console.log("somefile has been loaded!");</code></pre> Then import it <pre><code>// main.js&#13;const { sayHello } = require('./somefile.js');&#13;&#13;sayHello();&#13;&#13;// 👇 Outputs 👇&#13;// "somefile has been loaded!"&#13;// "Hello, world!"</code></pre></div> |
| **Full Example** | &#13;Make sure to export the function first. <pre><code>// somefile.js&#13;export function sayHello() {&#13; console.log("Hello, world!");&#13;}&#13;&#13;console.log("somefile has been loaded!");</code></pre> Then import it <pre><code>// main.js&#13;import { sayHello } from './somefile.js';&#13;&#13;sayHello();&#13;&#13;// 👇 Outputs 👇&#13;// "somefile has been loaded!"&#13;// "Hello, world!"</code></pre> | &#13;Make sure to add the function to `module.exports`. <pre><code>// somefile.js&#13;function sayHello() {&#13; console.log("Hello, world!");&#13;}&#13;&#13;module.exports = { sayHello };&#13;&#13;console.log("somefile has been loaded!");</code></pre> Then import it <pre><code>// main.js&#13;const { sayHello } = require('./somefile.js');&#13;&#13;sayHello();&#13;&#13;// 👇 Outputs 👇&#13;// "somefile has been loaded!"&#13;// "Hello, world!"</code></pre> |
| **Scope** | <div><p>If an exported value changes in the module it was defined in, that change is visible in all modules that import this value.<p>For example: <pre><code>// somefile.js&#13;let count = 1;&#13;export { count };&#13;&#13;setTimeout(() => count = 2, 1000);</code></pre> Now use it somewhere <pre><code>// main.js&#13;import { count } from './somefile.js';&#13;&#13;console.log(count); // 1&#13;setTimeout(() => console.log(count), 1000); // 2</code></pre></div> | <div><p>The exports are _copied_ at the time of requiring the module.<br>So even if an exported value changes in the module it was defined in, that change is **not** visible in the module where it's required.</p> For example: <pre><code>// somefile.js&#13;let count = 1;&#13;module.exports.count = count;&#13;&#13;setTimeout(() => count = 2, 1000);</code></pre> Now use it somewhere <pre><code>// main.js&#13;const mod = require('./somefile.js');&#13;&#13;console.log(mod.count); // 1&#13;setTimeout(() => console.log(mod.count), 1000); // 1</code></pre></div> |
| **Scope** | &#13;<p>If an exported value changes in the module it was defined in, that change is visible in all modules that import this value.<p>For example: <pre><code>// somefile.js&#13;let count = 1;&#13;export { count };&#13;&#13;setTimeout(() => count = 2, 1000);</code></pre> Now use it somewhere <pre><code>// main.js&#13;import { count } from './somefile.js';&#13;&#13;console.log(count); // 1&#13;setTimeout(() => console.log(count), 1000); // 2</code></pre> | &#13;<p>The exports are _copied_ at the time of requiring the module.<br>So even if an exported value changes in the module it was defined in, that change is **not** visible in the module where it's required.</p> For example: <pre><code>// somefile.js&#13;let count = 1;&#13;module.exports.count = count;&#13;&#13;setTimeout(() => count = 2, 1000);</code></pre> Now use it somewhere <pre><code>// main.js&#13;const mod = require('./somefile.js');&#13;&#13;console.log(mod.count); // 1&#13;setTimeout(() => console.log(mod.count), 1000); // 1</code></pre> |


If it's hard to read here, [read a copy of this table](https://github.com/akhanalcs/tour-of-heroes/blob/main/docs/learn-javascript.md#import-vs-require) in GitHub.
If it's hard to read here, [read a copy of this table](https://github.com/akhanalcs/tour-of-heroes/blob/main/docs/learn-javascript.md#import-vs-require) in GitHub.