I have this snippet below (with this valid case, where x.size exists):
let x = {
"size": 123
}
let y = {
'size': null
}
try {
y.size = parseFloat(x.size) ? `${parseFloat(x.size)}` : `${x.size}`;
} catch (e) {
console.log("yes")
y.size = null;
}
console.log(y)
But I was expecting in case the property x.size is missing to enter on catch, and set y.size to null, but I get:
{ size: undefined }
let x = {
}
let y = {
'size': null
}
try {
y.size = parseFloat(x.size) ? `${parseFloat(x.size)}` : `${x.size}`;
} catch (e) {
console.log("yes")
y.size = null;
}
console.log(y)
What am I doing wrong? Thank you for your time!
xexists and is an object, so there's no problem accessing its properties.x.sizeis indeedundefinedand there's nothing wrong with accessing or writing to it. If you want to test for its presence, use'size' in x