How To Get Current Line Number In JavaScript I will break this review in the following sections: The problem of why you may need to get the line number in Javascript Show an example use-case of the problem Demonstrate a solution with a bonus tip to resolve the problem
The problem is to identify a way to get the line number of the Javascript error.
To do this we basically have to handle the exception and print out the line number of the error object that we create in our handling code.
(async () => { let variable1 = 'test'; let variable2 = 'test11'; console.log('Starting to run code'); try { console.log(doesntexist); } catch (err){ const terr = new Error(); console.log('Failed line number: ', terr.lineNumber); }})(); The code above when executed will basically show a print out with the line number that failed.