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".

Learn JavaScript Today

Register today for the Beginner Basics course and get the new releases by just paying the difference. There's also special discounts for existing customers when new courses are released.


Still Not Sure? Check out more curriculum!