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

What is being sliced off from the query?

I'm a bit unclear as to why the initial query is being sliced. If I just type in 90210 as the argument to app.js, what is it that is being sliced off the process.argv array?

const query = process.argv.slice(2).join("").replace('', '');

1 Answer

Steven Parker
Steven Parker
202,001 Points

The non-argument items are being removed.

The first item (index 0) is a reference to node itself, and the second one is a reference to app.js. By removing those, what remains are the arguments.

ah thank you, I see. Is that the same for all Node apps then?

If I had a Node app that took two arguments (node app.js argument1 argument2)in the terminal command, would I want the statement to read as follows?: process.argv.slice(2, 3)

Steven Parker
Steven Parker
202,001 Points

You might, if you want to ignore a third (or more) arguments. A slice using a single argument of "2" would get all provided arguments (however many there were).