Explain and give example of all the javascript errors with example and it should be in detail and code should be format and give the example to avoid the errors.
A:
SyntaxError: This error occurs when the syntax of a code is incorrect. For example, if you forget to close a bracket or a parenthesis, you will get a SyntaxError.
Example:
<script>
var x = 10;
if (x > 5)
console.log("x is greater than 5");
</script>
This code will result in a SyntaxError because the if statement is missing a closing bracket. The correct code should look like this:
<script>
var x = 10;
if (x > 5) {
console.log("x is greater than 5");
}
</script>
ReferenceError: This error occurs when a reference to a variable or function is made in the code, but the variable or function is not defined.
Example:
<script>
console.log(x);
</script>
This code will result in a ReferenceError because the variable x is not defined.
TypeError: This error occurs when a value is not of the expected type.
Example:
<script>
var x = 10;
x.toUpperCase();
</script>
This code will result in a TypeError because the value of x is a number, not a string.
RangeError: This error occurs when a value is not within the expected range.
Example:
<script>
var x = new Array(100000000);
</script>
This code will result in a RangeError because the size of the array is too large.
URIError: This error occurs when a malformed URI is used.
Example:
<script>
decodeURI("%");
</script>
This code will result in a URIError because the URI is malformed.