Hint: catch {} valid in modern JS with optional binding [OK]
Common Mistakes:
Thinking catch always requires parameter
Believing syntax error without catch param
Confusing with pre-ES2019 JS
5.
Consider this code:
let result = '';
function test() {
try {
result += 'Try';
throw new Error('Error!');
} catch (e) {
result += 'Catch';
return result;
} finally {
result += 'Finally';
}
}
test();
console.log(result);
What will be printed to the console?
hard
A. TryCatchFinally
B. TryCatch
C. TryFinally
D. Try
Solution
Step 1: Trace try block execution
result becomes 'Try', then error is thrown.
Step 2: Catch block runs and appends 'Catch', then returns result ('TryCatch')
Before returning, finally runs and appends 'Finally' to result.
Step 3: Check console.log output
console.log prints result after finally appended, so 'TryCatchFinally'.
Final Answer:
TryCatchFinally -> Option A
Quick Check:
finally runs before return completes = B [OK]
Hint: finally runs before return completes, modifies variables [OK]
Common Mistakes:
Assuming return stops finally from running
Expecting console.log to print before finally modifies result
Ignoring that finally runs even after return
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.