Uncaught TypeError: Cannot read property of undefined In JavaScript
Tags: javascript, beginners, webdev, showdev
Published: 2019-01-31 09:40:45

---
title: Uncaught TypeError: Cannot read property of undefined In JavaScript
published: true
description: Out of the six primitive types defined in JavaScript, namely boolean, string, symbol, number, Null, and undefined, no other type throws as many errors as Undefined.The error most often than not is faced when the scripts come across uninitialized variable or object.
tags: #javascript #beginners #webdev #showdev
cover_image: https://www.lambdatest.com/blog/wp-content/uploads/2018/03/TE.jpg
canonical_url: https://www.lambdatest.com/blog/undefined-type-error-in-javascript/
---

Out of the six primitive types defined in JavaScript, namely boolean, string, symbol, number, Null, and undefined, no other type throws as many errors as Undefined.

The error most often than not is faced when the scripts come across uninitialized variable or object.

###Description###
‘Undefined’ is the property of the global object. If you don’t assign any value to a variable is of type ‘undefined’. A code also return the undefined value when the evaluated variable doesn’t have any assigned value.

###Code structure###
<pre>
function test(t) {      //defining a function
  if (t === undefined) {       //if t=undefined, call tt
        console.log(t.tt)      //call tt member from t
  }
  return t;    
}

var a;    //a is a variable with undefined value

console.log(test(a)); //function call
</pre>

###Error###

When you run this code, you’ll get:

![](https://www.lambdatest.com/blog/wp-content/uploads/2018/03/undefined1.png)

###Debugging###
If you get undefined error, you need to make sure that which ever variables throws undefined error, is assigned a value to it.

<pre>function test(t) {      //defining a function
  if (t === undefined) {       //if t=undefined, call tt
        console.log(t)      //call t
  }
  return t;    
}

var a=10;    //a is a variable with undefined value

console.log(test(a)); //function call
</pre>

After I assign a value to a, the function will return me the value of t, mapped by a=10.

<strong>OutPut= 10</strong>

Catch Before It Happens.

Ability to work with uninitiated variables/objects is one of the plus points of JavaScript over other languages like Java, C etc. So many times developers are tempted to use this feature, even though it not a very good practice. So if you are a developer that is addicted to not initializing the variables, you can at least attempt to catch the problems before they happen through simple statement

<pre>if (typeof(jsvariable) == 'undefined') {
  ...
}
</pre>

Hope this solves a bit of your problem. If you face any error or have alternative to this one, let us know in the comment section below.

Original Source: [LambdaTest Blog](https://www.lambdatest.com/blog/undefined-type-error-in-javascript/)

Related Post:
1. [(Unknown): Script Error in JavaScript](https://www.lambdatest.com/blog/script-error-in-javascript/)
2. [SyntaxError: JSON.parse: bad parsing](https://www.lambdatest.com/blog/syntaxerror-json-parse-bad-parsing/)
3. [Typography And Cross Browser Compatibility Testing](https://www.lambdatest.com/blog/typography-and-cross-browser-compatibility/)