JavaScript Node.js Basics Create a Command Line Weather Application Retrieving Data - Solution

Sean Flood
Sean Flood
13,370 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?

1 Answer

Steven Parker
Steven Parker
179,664 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").