0

I have been doing quite a bit of JS recently, and although I usually get things to work via trial and error, I don't really understand why or when to use curly brace imports vs just "plain" imports?

import { thing } from 'file'

import otherThing from 'different-file'

I'm just wondering what the difference is between the two and how I can tell when to use one vs the other? I'm sure this was explained in some lesson I had, but I must have memory-holed it or just not paid attention.

I always use NodeJS, not sure if that really makes a difference in the context of this question.

nos codemos
  • 569
  • 1
  • 5
  • 19
  • 1
    https://stackoverflow.com/q/36795819/4025157 – MC Naveen Jun 28 '22 at 10:53
  • A short answer would be `{ x } = { x : 7 }` has the effect of x = 7. Whereas `x = { x : 7 }` would have the effect of x = { x : 7 } – IT goldman Jun 28 '22 at 11:01
  • 1
    @ITgoldman this is not accurate. `import x from 'foo'` does not actually get all named exports. It only gets the *default export* which does not need to match any of the named ones. Consider `export const x = 7; export const y = 8; export default "hello";` then `import { x } from 'foo'` is x = 7; however, `import x from 'foo'` is x = "hello". `import * as x from 'y'` would be closer what you show in your second code. Destructuring syntax and imports are not one and the same. – VLAZ Jun 28 '22 at 11:09
  • Thanks for the clarification. I hope that my example at least accurate on it's own without the import. – IT goldman Jun 28 '22 at 11:12

0 Answers0