Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

Jesse Thompson
Jesse Thompson
10,684 Points

Little confused with how users.forEach(profile.get); grabs users for get function

So my app functions perfectly but I am confused as to how some of the syntax works.

In my app.js file I have the following

`javascript

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

// process is a native object that takes command line words to be passed as arguments after node whateverapp.js is written. // this is put into a variable and a forEach function loop is ran to run the getProfile function with each user passed as the argument for username. const users = process.argv.slice(2); users.forEach(profile.get); ` and in the profile.js I have pretty much exactly what Andrew has in his file.

For the last line in the app file. The users variable is passed as an argument to the get function? The syntax here is so weird its hard for me to make out. Im expecting something like profile.get(users));

1 Answer

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

users is not in parens anywhere so it is not itself being passed as an argument. what is happening here is what is described in the comments - process.argv is an array and you are calling the native JS array method slice on it to get all but the first 2 items, which are the profile names entered when you run the app from the command line. these names are in an array assigned to the users variable. then, the native array method forEach is called on the users array, and for each name in the users array, profile.get is called.