Import and require in JavaScript: a simple example

Export/import before ES6:

javascript
function sum(a, b){
    
    return a + b;
}

function sub(a, b){
    
    return a - b;
    
}

module.exports = { sum, sub };

javascript
const ({ sum, sub }) = require('./Math');

Export/import after ES6:

javascript
export const sum = (a, b) => a + b;
export const sub = (a, b) => a - b;

javascript
import { sum, sub } from './Math'; 

Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.