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 trialChris Mason
Full Stack JavaScript Techdegree Graduate 24,526 PointsWhat 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
231,236 PointsThe 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.
Chris Mason
Full Stack JavaScript Techdegree Graduate 24,526 PointsChris Mason
Full Stack JavaScript Techdegree Graduate 24,526 Pointsah 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
231,236 PointsSteven Parker
231,236 PointsYou 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).