Question about importing modules

@midlindner
I have a question about this. In the lesson it asked when creating the import to have the arguments listed. As seen below

/* water-limits.js */
const converters = require('./converters.js');
 
const freezingPointC = 0;
const boilingPointC = 100;
 
const freezingPointF = converters.celsiusToFahrenheit(freezingPointC);
const boilingPointF = converters.celsiusToFahrenheit(boilingPointC);
 
console.log(`The freezing point of water in Fahrenheit is ${freezingPointF}`);
console.log(`The boiling point of water in Fahrenheit is ${boilingPointF}`);

When you are assigning the const freezingPointF it has a place holder for the arguments into the function.
as well as in this one

/* celsius-to-fahrenheit.js */
const { celsiusToFahrenheit } = require('./converters.js');
 
const celsiusInput = process.argv[2]; 
const fahrenheitValue = celsiusToFahrenheit(input);
 
console.log(`${celsiusInput} degrees Celsius = ${fahrenheitValue} degrees Fahrenheit`);

With the const fahrenheitValue = celsiusToFahrenheit(input)

However in the very next assignment is does not have you do this. Why is this this way or am I missing something. Do i always save it to a const that does not have the list of arguments?

 const encryptors = require('./encryptors.js');

const caesarCipher = encryptors.caesarCipher;
const symbolCipher = encryptors.symbolCipher;
const reverseCipher = encryptors.reverseCipher;

I am fine with accepting and doing the last way I was just curious as to why they had me place the arguments in the first one in the example if this is not how it is going to normally be. The only conclusion I can come to is that they are creating the variable and calling the function on the same line? I hope I am making sense with my question.

Hello, @degsx20845821697.

Not sure I fully understand what you’re asking, but I’ll take a stab at providing an answer.

In your first example, you import a module that contains a celciusToFahrenheit function, and assign the const variable converters to the imported module. You then assign the variables freezingPointC and boilingPointC to the integer values, 0 and 100 respectively. Then you assign freezingPointF to the return value of a function call. The function is in the imported module, so you access it using dot notation:

converters.celsiusToFahrenheit(freezingPointC);
//module      function name       argument

Same procedure is used in assigning boilingPointF

In the next example, rather than importing the entire module, you only import the function celsiusToFahrenheit using object destructuring. Then fahrenheitValue is assigned to the return value of calling that function using input as the argument. Note: This example provided by the lesson would throw an error if executed since input is undefined. It should have been celsiusInput.

In your third example, an entire module is once again imported with the name encryptors assigned to it. Then, probably to make calling the functions later a bit easier, three functions in the module have individual const variables assigned to them:

None of these functions have been called at this point. The only thing that has occurred is that the shorter variable names can now be used to refernce the functions without having to include the name of the module. This could also have been accomplished on a single line of code using object destructuring as in the previous example:

const {caesarCipher, symbolCipher, reverseCipher} = require('./encryptors.js')

Both methods allow you to then call the functions without referencing the module:

const someVariable = caesarCipher(someString, amount);

instead of:

const someVariable = encryptors.caesarCipher(someString, amount);
1 Like

Ok so I think I understand what you are saying and I believe you answered my question. At the end of my post I had stated that the only conclusion I could come to was because the function was being called while it was also being placed in a variable. As I started to type my question to you I was still unsure of why but as I continued to ask the question I thought that might have been the reason which you clarified for me. Thanks. I also saw that error in the second example not using celsiusInput and thought that was wrong too but that was how it was listed on the course material. Thanks for your help and answer it has helped to clarify.

1 Like