0
0
Node.jsframework~20 mins

process.env for environment variables in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Environment Variable 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 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);
Anull
BThrows ReferenceError
Cundefined
D"hello"
Attempts:
2 left
💡 Hint
Remember that process.env stores environment variables as strings.
Predict Output
intermediate
2: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);
A"" (empty string)
Bundefined
Cnull
DThrows ReferenceError
Attempts:
2 left
💡 Hint
Think about what process.env returns for missing keys.
component_behavior
advanced
2: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?
AIt is available only in the current process and can be accessed anywhere in the code.
BIt updates the system environment variables permanently.
CIt throws an error because environment variables are read-only.
DIt is only available inside the function where it was set.
Attempts:
2 left
💡 Hint
Think about the scope of process.env in a running Node.js process.
📝 Syntax
advanced
2: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.
Aprocess.env.API_KEY = 'abc123';
Bprocess.env['API_KEY'] = abc123;
Cprocess.env.API_KEY := 'abc123';
Dset process.env.API_KEY = 'abc123';
Attempts:
2 left
💡 Hint
Remember how to assign values to object properties in JavaScript.
🔧 Debug
expert
3: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';
ABecause process.env is immutable and throws an error when assigned.
BBecause environment variables cannot be changed at runtime.
CBecause the environment variable is logged before it is set.
DBecause the variable name is case-sensitive and should be lowercase.
Attempts:
2 left
💡 Hint
Look at the order of the statements.