process.argv command line arguments

suggest change

process.argv is an array containing the command line arguments. The first element will be node, the second element will be the name of the JavaScript file. The next elements will be any additional command line arguments.

Code Example:

Output sum of all command line arguments

index.js

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

console.log(sum);

Usage Exaple:

node index.js 2 5 6 7

Output will be 20

A brief explanation of the code:

Here in for loop for (i = 2; i < process.argv.length; i++) loop begins with 2 because first two elements in process.argv array always is ['path/to/node.exe', 'path/to/js/file', ...]

Converting to number Number(process.argv[i]) because elements in process.argv array always is string

Feedback about page:

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


Environment:
* process.argv command line arguments

Table Of Contents
1 npm
12 Environment
41 cli
43 grunt
59 Hack
64 ES6
67 Redis
69 MongoDB
86 MongoDB
87 Lodash
91 CORS
105 N-API
108 Require