Uncaught TypeError: Cannot read property of undefined

JavaScript TypeError is thrown when an operand or argument passed to a function is incompatible with the type expected by that operator or function. This error occurs in Chrome Browser when you read a property or call a method on an undefined object . There are a few variations of this error depending on the property you are trying to access. Sometimes instead of undefined it will say null.
How to handle Uncaught TypeError: Cannot read property of undefined in Javascript
example
function myFunc(inVar) { if (inVar === undefined) { console.log(inVar.not) } return inVar; } var testVar; console.log(myFunc(testVar));

When you run this code, you will get this message in browser:

Uncaught TypeError: Cannot read property 'not' of undefined
In basic terms, undefined means that a variable has been declared but has not yet been assigned a value. This particular error is probably easiest to understand from the perspective of undefined, since undefined is not considered an object type at all (but its own undefined type instead), and properties can only belong to objects within JavaScript. In the above code, when you get undefined error , you need to make sure that whichever variables throws undefined error, is assigned a value to it. example
function myFunc(inVar) { if (inVar === undefined) { console.log(inVar.not) } return inVar; } var testVar=99; //initialize here console.log(myFunc(testVar));
In the above JacaScript you can the variable testVar is initialized the value of 99. So the script run successfully.

Handling undefined


hadling Uncaught TypeError: Cannot read property of undefined in Javascript
You can handle undefined by using if statement.
if (typeof(yourvariable) == 'undefined') { ... }

JavaScript undefined


handling JavaScript undefined
Undefined means a variable has been declared, but the value of that variable has not yet been defined. In JavaScript there is Undefined ( type ), undefined ( value ) and undefined ( variable ).
var myVar; console.log(myVar);
output
undefined

Also, undefined is of the type undefined:

console.log(typeof myVar);
output
undefined

Moreover, declare a variable then assign undefined to it:

var myVar = undefined; console.log(test);
output
undefined