Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Using process.exit and Exit Codes in Node.js
📖 Scenario: You are creating a simple Node.js script that checks if a user is authorized to access a system. If the user is authorized, the script will exit with a success code. If not, it will exit with an error code.
🎯 Goal: Build a Node.js script that uses process.exit with different exit codes to indicate success or failure.
📋 What You'll Learn
Create a variable named isAuthorized with the value true or false.
Create a variable named exitCode to hold the exit code number.
Use an if statement to set exitCode to 0 if isAuthorized is true, otherwise set it to 1.
Call process.exit(exitCode) to end the program with the correct exit code.
💡 Why This Matters
🌍 Real World
Many command-line tools and scripts use exit codes to tell other programs if they worked or failed.
💼 Career
Understanding process.exit and exit codes is important for writing reliable Node.js scripts and tools that integrate well with other software.
Progress0 / 4 steps
1
Set up the authorization variable
Create a variable called isAuthorized and set it to false.
Node.js
Hint
Use const isAuthorized = false; to create the variable.
2
Create the exit code variable
Create a variable called exitCode and set it to 0 as a starting value.
Node.js
Hint
Use let exitCode = 0; to create the variable.
3
Set exit code based on authorization
Use an if statement to check if isAuthorized is true. If yes, set exitCode to 0. Otherwise, set exitCode to 1.
Node.js
Hint
Use if (isAuthorized) { exitCode = 0; } else { exitCode = 1; }.
4
Exit the process with the exit code
Call process.exit(exitCode) to end the program with the correct exit code.
Node.js
Hint
Use process.exit(exitCode); to exit the program.
Practice
(1/5)
1. What does process.exit(0) do in a Node.js program?
easy
A. Stops the program immediately and signals success
B. Stops the program immediately and signals an error
C. Pauses the program without exiting
D. Restarts the program automatically
Solution
Step 1: Understand process.exit behavior
process.exit immediately stops the Node.js program.
Step 2: Interpret exit code 0
An exit code of 0 means the program ended successfully without errors.
Final Answer:
Stops the program immediately and signals success -> Option A
Quick Check:
Exit code 0 = success [OK]
Hint: Exit code 0 means success, non-zero means error [OK]
Common Mistakes:
Thinking exit code 0 means error
Confusing pause with exit
Assuming process.exit restarts program
2. Which of the following is the correct syntax to exit a Node.js program with an error code 1?
easy
A. process.exit = 1;
B. exit.process(1);
C. process.exit(1);
D. process.exitCode = 1;
Solution
Step 1: Identify correct method to exit
The method to stop the program is process.exit() with a code inside parentheses.
Step 2: Check syntax correctness
process.exit(1); is the correct syntax to exit with code 1. Other options misuse method or assignment.