In JavaScript, the Error.prototype.lineNumber property helps us to determine which line in our code corresponds to an error. One important thing to note is that this property is not used extensively as it is not a standard feature.
Syntax:
errorVariable.lineNumber
Example 1:
Javascript
var ex_variable = 2; var er = new Error( "Example Error" ); if (ex_variable > 1) { throw er; } // Error is in the 5th line so log will show 5 console.log( "Error is in line number " + er.lineNumber); |
Output:
In the above example, the number 5 is printed in the logs as the 5th line is the line in which an error is thrown.
Example 2:
Javascript
window.addEventListener( "error" , function (er) { // Line number 7 throws an error, so output is 7 console.log( "The error is thrown in the line " + er.lineNumber); }); var ex_var = 3; var er = new Error( "Example error" ); if (ex_var < 5) throw er; |
Output:
When the error event is fired, the number 7 is printed in the logs as the 7th line is the line in which an error is thrown.
Supported Browser: The Error.prototype.lineNumber property can only run on Firefox.
- Firefox