Table of Contents
The Error Object provides error information when an error occurs. JavaScript has a built-in error object that provides error information when an error occurs. The error object provides two useful properties: name and message.
You can also create a new Error object with the new Error() constructor.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Error Reference</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>The Error Object</h4>
<p id="demo"></p>
<script>
// Create a new Error object
const err = new Error("Something went wrong!");
document.getElementById("demo").innerHTML =
"Error Name: " + err.name + "<br>" +
"Error Message: " + err.message;
</script>
</body>
</html>

Result View Example
The Error object has the following important properties:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Error Reference</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Error Object Properties & Methods</h4>
<p id="demo"></p>
<script>
let text = "";
try {
adddlert("Welcome guest!");
}
catch(err) {
text += "Error Name: " + err.name + "<br>";
text += "Error Message: " + err.message + "<br><br>";
text += "typeof err: " + typeof err;
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>

Result View Example
An EvalError indicates an error regarding the global eval() function. These errors are no longer thrown by JavaScript, but the EvalError object remains for compatibility.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Error Reference</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>EvalError</h4>
<p id="demo"></p>
<script>
let text = "";
try {
throw new EvalError("Hello", "someFile.js", 10);
}
catch(err) {
text += "Error Name: " + err.name + "<br>";
text += "Error Message: " + err.message;
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>

Result View Example
A RangeError is thrown when a value is not in the set or range of allowed values.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Error Reference</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>RangeError</h4>
<p id="demo"></p>
<script>
let text = "";
let num = 1;
try {
num.toPrecision(500); // A number cannot have 500 significant digits
}
catch(err) {
text += "Error Name: " + err.name + "<br>";
text += "Error Message: " + err.message;
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>

Result View Example
A ReferenceError is thrown when a variable that doesn't exist (hasn't been declared) is referenced.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Error Reference</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>ReferenceError</h4>
<p id="demo"></p>
<script>
let text = "";
try {
x = y + 1; // y is not defined
}
catch(err) {
text += "Error Name: " + err.name + "<br>";
text += "Error Message: " + err.message;
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>

Result View Example
A TypeError is thrown when an operation cannot be performed, typically when a value is not of the expected type.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Error Reference</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>TypeError</h4>
<p id="demo"></p>
<script>
let text = "";
try {
let num = 1;
num.toUpperCase(); // Numbers don't have toUpperCase()
}
catch(err) {
text += "Error Name: " + err.name + "<br>";
text += "Error Message: " + err.message;
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>

Result View Example
Document in project
You can Download PDF file.