TypeError: JavaScript
Tags: javascript, typeerror, java, programming
Published: 2019-01-28 13:21:32

---
title: TypeError: JavaScript
published: true
description: 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.
tags: #javascript #typeerror #java #programming
cover_image: https://thepracticaldev.s3.amazonaws.com/i/jp8s6sn0bsj7azudnhnf.PNG
canonical_url: https://www.lambdatest.com/blog/typeerror-javascript/
---

Can you add a number and an alphabet?

Say, if I ask you to give me the result of the addition of <code>1</code> and <code>H</code> 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 <code>TypeError</code>.

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.

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 <code>Uncaught TypeError</code> if you are trying to convert a number to uppercase character. As <code>toUpperCase()</code> is a function to convert a string to uppercase characters.It will give an error for following code structure.

<strong>Code structure</strong>
<pre>var num=1;
i.toUpperCase();
</pre>

<strong>Error</strong>

![Error](https://thepracticaldev.s3.amazonaws.com/i/i9r2b87dsie3oqak0avq.PNG)


###How to get rid of this Uncaught type error: Cannot set property###

There are many methods possible to overcome this error.

<strong>1. Using toString() function</strong>
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.

<pre>var num = 1;
try {

    num.toString().toUpperCase();   // Convert number into string first 
}
catch(err) {
    document.getElementById("demo").innerHTML = err.name;
}</pre>

<strong>Output</strong>: 1

<strong>2. Using constructor new String() of predefined class</strong>

<pre>var num = 1;
num=new String(num);
try {

    num.toUpperCase();   // You cannot convert a number to upper case
}
catch(err) {
    console.log(err.name);
}</pre>

<strong>Output</strong>: 1


Here are few more TypeError that can be thrown by JavaScript in different browsers.

<strong>TypeError related to console.log()</strong>
<pre>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)</pre>

<strong>TypeError related to prompt()</strong>
<pre>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)</pre>

<strong>TypeError related to confirm()</strong>
<pre>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)</pre>

Original Source: [lambdatest.com](https://goo.gl/Xn3QP1)