What should you choose: named imports or default imports?

A debate on the type of import we use in JavaScript has recently appeared. Same kind of debate than the one with semicolon. It works whichever you choose but each developer has its opinion on the matter and nobody agree. I also have an opinion!


This is a companion discussion topic for the original entry at https://blog.nathanaelcherrier.com/en/javascript-named-imports-vs-default-imports/
1 Like

New discussion on dev.to by Seanmclem:

I’m starting to prefer named imports. Defaults seem to create inconsistentcy for me so I have been trying to avoid them lately.

Source: https://dev.to/seanmclem/comment/gm0o

1 Like

New discussion on dev.to by Eddie Aich:

Great article! I agree that different situations may call for different techniques.

If I expect the functions in the module to be used often and holistically, I may use default export at the end of my file instead of defining exports on each function:

// Math.js
function add(x, y) {
  // ...
}

export default {
  add,
  subtract,
  multiply
}

import Math from './Math.js';

const result = Math.add(x, y);

…and I’ll name my module something generic like Math. Or in the example you showed above, Rotate instead of detectRotation.

Named exports may be more readable to some, but sometimes it may be preferable to see the function and the module in which it came from.

import { detect as detectRotation } from './Rotate.js';
detectionRotation();
// vs.
import Rotate from './Rotate.js';
Rotate.detect();

Source: https://dev.to/eaich/comment/gmjk

1 Like