August
16th,
2016
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
Usage Exaple:
Output will be 25
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