Uncaught TypeError: Cannot set property
In JavaScript almost everything is an object, null and undefined are exception. When you try to access an undefined variable it always returns undefined and we cannot get or set any property of undefined. In that case, an application will throw Uncaught TypeError cannot set property of undefined .
example 1
var array
array[0] = 10
console.log(array);
output
Uncaught TypeError: Cannot set property '0' of undefined
In JavaScript if a variable has been declared, but has not been assigned a value, is automatically assigned the value undefined . Therefore, if you try to access the value of such variable, it will throw Uncaught TypeError cannot set property of undefined .
example 2
var test = null
test[0] = 10
output
Uncaught TypeError: Cannot set property '0' of null
The null is a special assignment value, which can be assigned to a variable as a representation of no value . If you try to access the value of such variable, it will throw Uncaught TypeError cannot set property of undefined .

How to fix Uncaught TypeError: Cannot set property
In the above cases, you are missing the array initialization . So, you need to initialize the array first.
var array = []
After adding this line your code should work properly.
var array = []
array[0] = 10
console.log(array);
How can I determine if a variable is 'undefined' or 'null'?
In simple words you can say a null value means no value or absence of a value, and undefined means a variable that has been declared but no yet assigned a value. JavaScript null and undefined is one of the main reasons to produce a runtime errors . This happens because you don't check the value of unknown return variables before using it. If you are not sure a variable that will always have some value, the best practice is to check the value of variables for null or undefined before using them. If you just want to check whether there's any value, you can do:
if( value ) {
//do something
}
will evaluate to true if value is not:
- null
- undefined
- NaN
- empty string ("")
- 0
- false
if ( typeof(some_variable) !== "undefined" && some_variable !== null ) {
//deal with value
}
ECMAScript 5 and ECMAScript 6

In newer JavaScript standards like ES5 and ES6 you can just say
- Boolean(0) //false
- Boolean(null) //false
- Boolean(undefined) //false
if (Boolean(myvar)){
// Do something
}
Here null or empty string or undefined will be handled efficiently.
Related Topics
- Uncaught TypeError: 'undefined' is not a function
- XMLHttpRequest cannot load no 'access-control-allow-origin'
- TypeError: null is not an object
- Uncaught RangeError: Maximum call stack size exceeded
- SecurityError: Blocked a frame with origin from accessing a cross-origin frame
- Unable to get property undefined or null reference
- SyntaxError: Cannot use import statement outside a module
- What is UnhandledPromiseRejectionWarning