Process Arguments
Now let us dive deeper into node.js world. Inside the same file, let's try to write the code above. Execute it again from console. What do you see?
Well what you see is an array with 2 elements. And more specific 2 paths. But what are these? What have we logged to the console? What is this process.argv?
Well, in order to answer this questions we need to understand that every time we execute a node.js script from the command line we initialize a new process, with a unique process id. The process object (you can log it, but it's big) contains all the necessary information the current execution. Information about the process, the memory that is used, in order for this script to be executed and more.
Ok. But then what is this argv array? This stands for argument values. Well it seems that we can parameterize a whole script by passing arguments in command line every time we execute the script. This piece of information will travel inside the file and will be placed wherever the process.argv[given_index_here] is used. Exactly like we can parameterize a function with function arguments, only in this case it's for the whole script.
But now we didn't pass any arguments, right? What are these two values? It seems that node.js has already 2 argument values as defaults. The first one is the path to where the executable file for node.js has been saved to our system, and the second argument is the path to the current executed file. After that we can add more arguments and access them by index array as usual.
Attention: process.argv values inside this array are always in format of string. A conversion must be made inside the script if you want to perform another operation.
Let's see couple of examples.