process.argv command line arguments

suggest change

process.argv is an array of the command-line arguments. The first element is the path to the node executable, the second is the path to the JavaScript file, and the rest are the arguments you passed.

Code Example:

Output sum of all command line arguments

index.js

var sum = 0;
for (var i = 2; i < process.argv.length; i++) {
    sum += Number(process.argv[i]);
}

console.log(sum);

Usage Example:

node index.js 2 5 6 7

Output will be 20

A brief explanation of the code:

The loop starts at 2 because the first two elements are always the node executable and the script path, for example ['path/to/node.exe', 'path/to/js/file', ...].

Number(process.argv[i]) is required because every element of process.argv is a string.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents