Javascript - The finally Clause
📖 Read the lesson
Code Life
By Pierre Marsaa
Code That Must Run No Matter What
Imagine saving a game: you open a save slot, write the data, and must
always
close the slot afterward — whether the write succeeded or crashed.
With only
try
and
catch
, you would have to duplicate the closing line in both blocks:
↗️
Loading editor...
↶
▶️ Run
↷
The
finally
block removes that duplication. Whatever happens inside
try
and
catch
, the code inside
finally
is guaranteed to run
at the end, always
:
↗️
Loading editor...
↶
▶️ Run
↷
Verify