How to catch syntax errors in JavaScript?

JavascriptWeb DevelopmentFront End Technology

You cannot use try-catch blocks for handling syntax errors in JavaScript since it is thrown while the code is being parsed.


Use window.onerror as in the following code −

         window.onerror = function(e) {
            document.write('Error: ', e, '</br>')

Note − Ensure that the error function is defined in a separate <script> tag.

Updated on 23-Jun-2020 05:20:11