JavaScript Node.js Basics Handling Errors in Node Organizing Your Code with require

Michael Pashkov
Michael Pashkov
22,022 Points

Hi, did not get - get. Why get and not get()?

For example, In challenge was question How would you access the functionality from another file?

Given the following code in greeting.js: function sayGreeting() { console.log("Hello World"); }

module.exports.say = sayGreeting;

and write answer was - const greeting = require("./greeting"); greeting.say();

Why in our example users.forEach(profile.get); and not users.forEach(profile.get()); //where get - name of our function

2 Answers

Steven Parker
Steven Parker
171,136 Points

That quiz question is about module exporting, but you seem to be asking about "forEach" syntax. They are not related.

But the "forEach" method takes a function as an argument, and "profile.get" is the name of the function. If you wrote "profile.get()" that would invoke the function and return the result instead of referencing the function itself.