08: Command Line Arguments

A command line is where you type commands in your terminal (aka PowerShell on Windows). In this exercise you learn how to get information from users by letting them put arguments after your .js file name like this:

$ node ex08.js Zed 44 Blue

The words "Zed", "44", and "Blue" after the script named ex08.js get passed to the script so you can work with them. In Node this is in the process.argv variable, and this variable is a list of the items on the command line.

I'm going to avoid telling you what a list is and save that for later (when we talk about Arrays). For now, just know that argument 1 ("Zed") is at position 2 in the process.argv and you tell JavaScript you want position 2 with [2] after process.argv. Just remember that your arguments are "off by one".

80% Discount for Alpha 1

Register today for the early access release and get an 80% discount on the final release price. You'll get true lessons in true modules right now, and early access to content as it's released.

Still Not Sure? Check out more curriculum!