Lesson 20 of 20
Scope & Error Handling
Keep values predictable and handle failures.
Introduction
Block scope means let and const belong to the nearest pair of braces. Keeping values scoped reduces accidental changes.
try, catch and finally let an application respond to errors and still perform cleanup.
Key insight
Keep values predictable and handle failures.
Example
try {
const value = JSON.parse("{bad json}");
console.log(value);
} catch (error) {
console.log("Invalid JSON");
}This example shows the core scope & error handling idea in a small, runnable program.
Real-world example
Scope & Error Handling appears in everyday application code, scripts and APIs.
Test yourself
2-question check
1
What is the main idea in Scope & Error Handling?
2
Where can scope & error handling be useful?
Ready to check your understanding?
Answer every question, then submit your quiz.
Try it yourself
JavaScript compiler
Runs in your browser. Nothing is sent to a server.
Output appears here
Practice challenge
Catch an error when converting invalid JSON.