I am having trouble understanding how import works in Javascript. Is it not supposed to just import the named function.
Here are my 2 files -
test.js -
export const add = (a,b) => {
return a+b
}
export const subtract = (a,b) => {
return a-b
}
console.log(add(7,7))
console.log(subtract(6,7))
and index.js
import { add } from './test.js'
console.log(add(4,5));
When running node index.js here is the output
14
-1
9
Why is the import also running the function calls in test.js
Thanks for your help.
In js import works in a way that the imported modules are invoked first wherever you place your import statements in your file and after that your current file is executed. That's how js works!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With