Bird
0
0

What will be printed when this Node.js code runs?

medium📝 Predict Output Q4 of 15
Node.js - Error Handling Patterns
What will be printed when this Node.js code runs?
try {
  const data = JSON.parse('{ invalid json }');
  console.log('Success');
} catch (e) {
  console.log('Parsing failed');
}
AParsing failed
BSuccess
CSyntaxError thrown and uncaught
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Analyze JSON.parse input

    The string '{ invalid json }' is not valid JSON and will throw an error.
  2. Step 2: Check try-catch behavior

    The thrown error is caught by catch block, which logs 'Parsing failed'.
  3. Final Answer:

    Parsing failed -> Option A
  4. Quick Check:

    Invalid JSON triggers catch [OK]
Quick Trick: Invalid JSON throws error caught by catch [OK]
Common Mistakes:
  • Assuming invalid JSON logs 'Success'
  • Thinking error is uncaught and crashes app

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes