In JavaScript, TypeError occurs when “an operation is not performed, and a value is not the expected type”.
Syntax
new TypeError([message[, fileName[, lineNumber]]])
Parameters
All the parameters are optional.
- message: It is a Human-readable description of the error.
- filename: It is the file’s name containing the code that caused the exception.
- lineNumber: The line number of the code caused the exception.
Example
let num = 11;
try {
num.toUpperCase();
}
catch (err) {
console.log(err)
}
Output
TypeError: num.toUpperCase is not a function
at Object.<anonymous> (/Users/krunal/Desktop/code/node-examples/es/app.js:3:7)
at Module._compile (internal/modules/cjs/loader.js:722:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:733:10)
at Module.load (internal/modules/cjs/loader.js:620:32)
at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
at Function.Module._load (internal/modules/cjs/loader.js:552:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:775:12)
at startup (internal/bootstrap/node.js:300:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:826:3)
We got the error like TypeError: num.toUpperCase is not a function
In this example, we are trying to convert a number to a string, which is impossible, so we catch the TypeError.
If you add 11 and H in JavaScript or when you try to perform operations on two operands of unmatched types, JavaScript throws a TypeError.
We can get the name of the error using the error.name property.
let num = 11;
try {
num.toUpperCase();
}
catch (err) {
console.log(err.name)
}
Output
TypeError
TypeError Methods
The global TypeError contains no functions but inherits some methods through a prototype chain.
TypeError Properties
There are the following properties of TypeError.
TypeError.prototype.constructor
It specifies a function that created the instance’s prototype.
TypeError.prototype.message
It gives the Error message. Although ECMA-262 specifies that TypeError should provide its message property,
SpiderMonkey inherits Error.prototype.message.
TypeError.prototype.name
It gives the Error name. Inherited from Error.
TypeError.prototype.fileName
It gives the path to the file that raised this error. Inherited from Error.
TypeError.prototype.lineNumber
It gives a line number in the file that raised this error. Inherited from Error.
TypeError.prototype.columnNumber
It gives column numbers in the line that raised this error. Inherited from Error.
TypeError.prototype.stack
It gives Stack a trace. Inherited from Error.
let num = 11;
try {
num.toUpperCase();
}
catch (err) {
console.log(err.message)
console.log(err.stack)
console.log(err.name)
}
Output
num.toUpperCase is not a function
TypeError: num.toUpperCase is not a function
at Object.<anonymous> (/Users/krunal/Desktop/code/node-examples/es/app.js:3:7)
at Module._compile (internal/modules/cjs/loader.js:722:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:733:10)
at Module.load (internal/modules/cjs/loader.js:620:32)
at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
at Function.Module._load (internal/modules/cjs/loader.js:552:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:775:12)
at startup (internal/bootstrap/node.js:300:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:826:3)
How to Fix Uncaught TypeError: cannot set a property
The “Uncaught TypeError: Cannot Set a Property” error occurs in JavaScript when you try to set a value for a property that does not exist or cannot be modified.
Here are some steps you can take to fix this error:
-
Check if the property exists: Make sure the property you are trying to set exists in the object. If it doesn’t, you must add it before setting its value.
- Check if the property is read-only: Some objects’ properties are read-only and cannot be modified. If you try to set a value for a read-only property, you will get this error. Check the documentation for the object you are working with to see if the property is read-only.
-
Use the correct syntax: Make sure you use the correct syntax to set the property value. For example, if you are working with an array, you should use the index number to set the value for a specific element. If you are working with an object, you should use dot or bracket notation to set the value for a particular property.
- Check the scope: Make sure the object you are working with is in scope. If the object is not in scope, you may be unable to set its properties. Check the code to see if the object has been declared and initialized properly.
- Use the correct data type: Make sure that the value you are trying to set for the property is of the correct data type. For example, if the property expects a string value, you cannot set it to a number or a boolean.
- Use try-catch blocks: You can use try-catch blocks to handle the error if you cannot fix the problem. This will allow you to catch and handle the error gracefully instead of crashing the program.
let num = 11;
try {
num.toString().toUpperCase();
}
catch (err) {
console.log(err.message);
}
It won’t give any errors. Instead, it solved TypeError by converting the number to a string.
Javascript TypeError is not a function
Let’s take a scenario where you define a variable and then call that variable a function that causes this TypeError.
let num = 11;
try {
num();
}
catch (err) {
console.log(err.message);
}
Output
num is not a function
You can only call functions in JavaScript. However, you can call the Object properties.
TypeError: is not a constructor
JavaScript built-in function objects not identified as constructors that do not implement the [[Construct]] internal method unless specified in the description of the specific function.
Browser compatibility
- Google Chrome
- Edge
- Firefox
- Internet Explorer
- Safari
- Opera
That’s it.