Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Error.prototype.lineNumber Property

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;
}
// 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
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments