what is the significance of finally in javascript?

The finally block in JavaScript executes after the try and catch blocks, regardless of whether an error occurred or not. It is commonly used for cleanup tasks like closing connections, releasing resources, or resetting state.

Syntax

try {
    // Code that may throw an error
} catch (error) {
    // Runs only if try throws an error
} finally {
    // Always runs ? after try, or after catch
}

Block Combinations

  • try + catch + finally − All three blocks (most common)
  • try + catch − Without finally
  • try + finally − Without catch (finally still runs)
  • try alone is not valid − at least catch or finally is required

Example: finally Runs After Successful try

When no error occurs, finally still executes after the try block:

try {
    console.log("Try: executing normally");
    let result = 10 + 5;
    console.log("Try: result is", result);
} catch (error) {
    console.log("Catch: error occurred -", error.message);
} finally {
    console.log("Finally: always runs");
}
Try: executing normally
Try: result is 15
Finally: always runs

The catch block was skipped because no error occurred, but finally ran.

Example: finally Runs After Error

When an error is thrown, finally executes after the catch block:

try {
    console.log("Try: about to cause an error");
    let x = undeclaredVariable;  // ReferenceError
} catch (error) {
    console.log("Catch:", error.message);
} finally {
    console.log("Finally: cleanup runs regardless");
}
Try: about to cause an error
Catch: undeclaredVariable is not defined
Finally: cleanup runs regardless

Example: try + finally (No catch)

The finally block runs even without a catch. The error propagates after finally completes:

function riskyOperation() {
    try {
        console.log("Try: opening resource");
        throw new Error("Something went wrong");
    } finally {
        console.log("Finally: resource cleaned up");
    }
}

try {
    riskyOperation();
} catch (e) {
    console.log("Outer catch:", e.message);
}
Try: opening resource
Finally: resource cleaned up
Outer catch: Something went wrong

The finally ran before the error propagated to the outer catch.

Practical Use Case: Cleanup

The most common use of finally is ensuring cleanup code runs regardless of success or failure:

let loading = true;

try {
    console.log("Fetching data...");
    // Simulate an operation that may fail
    let data = JSON.parse('{"name": "John"}');
    console.log("Data:", data.name);
} catch (error) {
    console.log("Error:", error.message);
} finally {
    loading = false;
    console.log("Loading state reset to:", loading);
}
Fetching data...
Data: John
Loading state reset to: false

Key Points

  • finally executes regardless of try/catch outcomes
  • Essential for cleanup operations and resource management
  • Runs even when errors are uncaught (in try-finally)
  • Cannot be used alone - requires either catch or finally with try

Conclusion

The finally block guarantees execution after try/catch, whether or not an error occurred. Use it for cleanup tasks like resetting loading states, closing connections, or releasing resources.

Updated on: 2026-03-15T19:55:47+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements