Challenge - 5 Problems
Environment Variable Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Node.js code using process.env?
Consider this code snippet that reads environment variables using
process.env. What will it print?Node.js
process.env.TEST_VAR = 'hello';
console.log(process.env.TEST_VAR);Attempts:
2 left
💡 Hint
Remember that
process.env stores environment variables as strings.✗ Incorrect
The code sets process.env.TEST_VAR to the string "hello" and then logs it. So it prints "hello".
❓ Predict Output
intermediate2:00remaining
What happens if you try to access a non-existent environment variable?
What will this code print if
process.env.MY_VAR was never set?Node.js
console.log(process.env.MY_VAR);
Attempts:
2 left
💡 Hint
Think about what
process.env returns for missing keys.✗ Incorrect
If an environment variable is not set, process.env returns undefined.
❓ component_behavior
advanced2:00remaining
How does changing process.env affect running Node.js code?
If you set
process.env.NEW_VAR = '123' inside your Node.js program, what happens to this variable?Attempts:
2 left
💡 Hint
Think about the scope of
process.env in a running Node.js process.✗ Incorrect
Setting process.env.NEW_VAR changes the environment variable only for the current Node.js process. It is accessible anywhere in that process but does not affect the system environment.
📝 Syntax
advanced2:00remaining
Which option correctly sets an environment variable in Node.js code?
Choose the correct way to set an environment variable named
API_KEY to abc123 inside a Node.js script.Attempts:
2 left
💡 Hint
Remember how to assign values to object properties in JavaScript.
✗ Incorrect
Option A uses correct JavaScript syntax to assign a string value to process.env.API_KEY. Option A misses quotes around the string, C uses invalid operator, and D uses invalid syntax.
🔧 Debug
expert3:00remaining
Why does this code not print the updated environment variable?
Given this code snippet, why does
console.log print undefined instead of the expected value?Node.js
console.log(process.env.MY_VAR);
process.env.MY_VAR = 'test';Attempts:
2 left
💡 Hint
Look at the order of the statements.
✗ Incorrect
The code logs process.env.MY_VAR before it is assigned a value, so it prints undefined. The assignment happens after the log.