TypeError: JavaScript
Saif Sadiq
Posted On: April 6, 2018
23263 Views
2 Min Read
Can you add a number and an alphabet?
Say, if I ask you to give me the result of the addition of 1 and H
will you be able to give me the answer?
The obvious answer is NO.
Same goes in JavaScript too!If you add 1 and H in JavaScript or when you try to perform operations on two operands of unmatched type, JavaScript throws a TypeError
.
So, you can say in technical terms that ‘TypeError is thrown when an operand or argument passed to a function is incompatible with the type expected by that operator or function’.
Therefore, it becomes necessary to make sure variables must have same data types before performing any operation.Type mismatch generates an error while executing the whole program.
Types of TypeError
For example, you will get Uncaught TypeError
if you are trying to convert a number to uppercase character. As toUpperCase()
is a function to convert a string to uppercase characters.It will give an error for following code structure.
Code structure
1 2 3 4 5 6 7 |
var num=1; try{ num.toUpperCase(); } catch(err) { document.getElementById("demo").innerHTML = err.name; } |
Error
How to get rid of this Uncaught type error: Cannot set property
There are many methods possible to overcome this error.
1.Using toString() function
You can use toString() function to convert number into string first and then you can convert that string to upper case characters using toUpperCase() function.
1 2 3 4 5 6 7 8 |
var num = 1; try { num.toString().toUpperCase(); // Convert number into string first } catch(err) { document.getElementById("demo").innerHTML = err.name; } |
Output
1 |
"1" |
2. Using constructor new String() of predefined class
1 2 3 4 5 6 7 8 9 |
var num = 1; num=new String(num); try { num.toUpperCase(); // You cannot convert a number to upper case } catch(err) { document.getElementById("demo").innerHTML = err.name; } |
Output
1 |
"1" |
Here are few more TypeError that can be thrown by JavaScript in different browsers.
TypeError related to console.log()
1 2 3 4 |
TypeError: Property 'log' of object # is not a function (Chrome) TypeError: console.log is not a function (Firefox) TypeError: 'your string' is not a function (evaluating 'console.log("your string")') (Safari) TypeError: Function expected (IE) |
TypeError related to prompt()
1 2 3 4 |
TypeError: Property 'prompt' of object [object Object] is not a function (Chrome) TypeError: prompt is not a function (Firefox) TypeError: 'a string, this could vary' is not a function (evaluating 'prompt("your question")') (Safari) TypeError: Function expected (IE) |
TypeError related to confirm()
1 2 3 4 |
TypeError: Property 'confirm' of object [object Object] is not a function (Chrome) TypeError: confirm is not a function (Firefox) TypeError: 'a string, this could vary' is not a function (evaluating 'confirm("your question")') (Safari) TypeError: Function expected (IE) |
Got Questions? Drop them on LambdaTest Community. Visit now