JavaScript Error.prototype.lineNumber Property
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;
}
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) {
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.
Last Updated :
28 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...