Skip to content

JavaScript TypeError

  • by

TypeError is a common type of error that occurs in JavaScript when the type of a variable or value is not what was expected. This error is typically thrown when an operation is performed on a value that is not compatible with the operation, or when a function is passed an argument of the wrong type.

JavaScript TypeError example

Simple example code of most Common TypeErrors and how to fix them in JavaScript. When you attempt to change a value that cannot be changed or use a value in an inappropriate way, you may encounter a TypeError.

<!DOCTYPE html>
<html>
<body>
    <script>
    const a = 5
    a = "5" 
    </script>
</body>
</html>

Output:

JavaScript TypeError

You can fix this by simply changing the name of the identifier.


const a = 5
const b = "5"

You will get Uncaught TypeError if you are trying to convert a number to an uppercase character. As toUpperCase() is a function to convert a string to uppercase characters.

var num = 11;
try{
num.toUpperCase();
}
catch(err) {
    document.getElementById("demo").innerHTML = err.name;
}

You can get the name of the error using the error.name property.

let num = 11;

try {
  num.toUpperCase();
}

catch (err) {
  console.log(err.name)
}

Do comment if you have any doubts on this Js error topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *