#TIL
- To use functions that are in a
non-main.js
script file, you must useexport
andimport
keywords. In thenon-main.js
file, you must designate the function you want to export by puttingexport
keyword in front of the function. - To successfully import the function to the
main.js
file, you need to explicitly stateimport { nameOfFunction } from 'path'
. - Reminder that you must use both export and import - they go in pairs.
Example:
non-main.js file
export function addTwo(number) {
const sum = number + 2;
return sum;
}
main.js file
import { addTwo } from './non-main.js'
addTwo(5);