If accessing properties of undefined value, I'm getting an exception:
let object = {}
let n = object["foo"].length;
VM186:1 Uncaught TypeError: Cannot read property 'length' of undefined at :1:12
I want to get a default value in this case instead of an exception, but the way I'm doing it now seems too verbose:
let n = 0;
if (object.hasOwnProperty("foo")) {
n = object["foo"].length;
}
Is there a more simple and elegant way to do this? Possibly, using ES6.