I came around a simple behavior that I cannot quite understand. I am trying to check for the validity of a JSON array of objects, the core point here is that the user 'choses' its size (for example, if he types 2, there will be 2 objects).
Each object is supposed to have a name and a version.
I wanted to be sure that he enters something, so when he hits he submit button, I'm checking for this kind of statement:
if (myarray.name !== undefined || myarray.name !== null)
// Do some crazy stuff as crazy a return;
console.log(myarray); // Prints '' because it is empty
console.log(myarray); // Prints 'undefined', seems legit
console.log(myarray.name); // Prints ... Well, nothing
Why does the third
console.logdoes not print
Because it throws an exception:
TypeError: Cannot read property 'name' of undefined. That happens when you try to read a property from