When you run a Node program, you can pass extra words after the file name. Node puts them in an array called process.argv (short for "argument values"):
console.log(process.argv);
node greet.js Omar 25
Output
[ '/usr/local/bin/node', '/home/you/node-practice/greet.js', 'Omar', '25' ]
The first two items are always the path to Node itself and the path to your file. The words you typed start at index 2. So usually you'll slice them off:
const [name = "stranger", age] = process.argv.slice(2);
if (age) {
console.log(`Hello, ${name}! Next year you'll be ${Number(age) + 1}.`);
} else {
console.log(`Hello, ${name}!`);
}
node greet.js Omar 25
Output
Hello, Omar! Next year you'll be 26.
(Destructuring with a default value, from Grade 13! And notice we converted age with Number(), because command-line arguments are always strings, just like prompt and input values.)
This is how command-line tools work. Every time you've typed something like node --version, the --version part was a command-line argument.