what is the significance of finally in javascript?

JavascriptObject Oriented ProgrammingProgramming

Finally

The finally statement lets you execute code, after try and catch, regardless of the result.The following example would give result from finally block even though catch block got the error. 

Example

 Live Demo

<html>
<body>
<pre>
<script>
   function addition() {
      document.writeln("every one should be good at addition");
   }
   function substraction(){
      document.writeln("substraction helps you");
   }
   try {
      addition()
      division()
   }
   catch(err) {
      document.writeln("be good at division : " + err);
   }
   finally {
      substraction()
   }
</script>
</pre>
</body>
</html>

Output

every one should be good at addition
be good at division : ReferenceError: division is not defined
substraction helps you
raja
Published on 21-May-2019 12:49:43
Advertisements