#TIL
- To use functions that are in a
non-main.jsscript file, you must useexportandimportkeywords. In thenon-main.jsfile, you must designate the function you want to export by puttingexportkeyword in front of the function. - To successfully import the function to the
main.jsfile, 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);