JS - 3 ways to import module

25 Sep 2020

// greeting.js
export function hello() {
  return 'hello';
}

export default function hi() {
  return 'hi';
}

1. Import a specific “export”

import { hello } from './greeting.js'

hello();

2. Import the default “export”

import hi from './greeting.js

hi();

2.1 Import with a specific and default “export” at same time

import hi, { hello } from '.greeting.js'

hi();
hello();

3. Import the module as a whole

import * as Greeting from './utils'

Greeting.hello(); //=> "hello"
Greeting.default(); //=> "hi"
Back to top