0
0
Node.jsframework~20 mins

process.argv for command line arguments in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Node.js Command Line Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Node.js script?
Consider this Node.js script run with the command: node script.js hello world. What will it print?
Node.js
console.log(process.argv.slice(2));
A["hello world"]
B["node", "script.js", "hello", "world"]
C["script.js", "hello", "world"]
D["hello", "world"]
Attempts:
2 left
💡 Hint
Remember that process.argv includes the node executable and script path as the first two items.
state_output
intermediate
2:00remaining
What is the value of args after running this code?
If the script is run as node app.js --name=John --age=30, what is the value of args?
Node.js
const args = process.argv.slice(2);
console.log(args);
A["--name=John", "--age=30"]
B["name", "John", "age", "30"]
C["--name", "John", "--age", "30"]
D["app.js", "--name=John", "--age=30"]
Attempts:
2 left
💡 Hint
Arguments are passed as strings exactly as typed after the script name.
📝 Syntax
advanced
2:00remaining
Which option correctly extracts the third command line argument?
Given the command node index.js a b c d, which code correctly assigns the third argument to variable thirdArg?
Aconst thirdArg = process.argv[2];
Bconst thirdArg = process.argv[4];
Cconst thirdArg = process.argv.slice(2)[3];
Dconst thirdArg = process.argv.slice(3);
Attempts:
2 left
💡 Hint
Remember that process.argv[0] is node path, [1] is script path, so arguments start at index 2.
🔧 Debug
advanced
2:00remaining
Why does this code print an empty array when run with arguments?
Given this code and running node app.js test, why does it print []?
Node.js
const args = process.argv.slice(3);
console.log(args);
ABecause slice(3) returns the first three elements, which are empty.
BBecause process.argv is empty when arguments are passed.
CBecause slice(3) skips the first three elements, but only two exist after index 2.
DBecause console.log cannot print arrays from process.argv.
Attempts:
2 left
💡 Hint
Check how many elements process.argv has when running with one argument.
🧠 Conceptual
expert
2:00remaining
What is the length of process.argv when running node without a script?
If you run node alone (no script, no arguments), what is the length of process.argv inside the Node.js REPL?
A2
B0
C3
D1
Attempts:
2 left
💡 Hint
Think about what process.argv contains by default: node path and script path.