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!

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 Create a Command Line Weather Application Retrieving Data - Solution

Swan The Human
Swan The Human
Full Stack JavaScript Techdegree Student 19,338 Points

Why are you joining the spaces in the query?

Just wondering what the purpose of the const query = process.argv.slice(2).join(' '); is.

Im sure an explanation will explain it properly but can anyone show me an example of whats being joined and why?

2 Answers

Steven Parker
Steven Parker
228,026 Points

In the video, the "join" uses and underscore ("_") instead of a space, but the concept is the same.

The special object attribute "process.argv" stores the terms that were used on the command line in an array, and "slice" with an argument of 2 removes the first two terms and returns the rest. So if the program was started with the command "node myprogram Cleveland Ohio", then the slice would return the array: ["Cleveland", "Ohio"]. Then the "join" converts the array into a single string, which in this case would be "Cleveland Ohio" (the one in the video would make "Cleveland _Ohio").

As of 2021 the available teacher's notes recommend in app.js const query = process.argv.slice(2).join(" ");