I have a lot of pieces of JavaScript like
if(myobject != undefined && myobject.someprop == something)
and I'm wondering if there exists syntax to make this more compact (even if it's a "hack" of some sort).
I have a lot of pieces of JavaScript like
if(myobject != undefined && myobject.someprop == something)
and I'm wondering if there exists syntax to make this more compact (even if it's a "hack" of some sort).
You can do simply like:
if(myobject && myobject.someprop == something)
Or typeof
validation:
if(typeof myobject != 'undefined' && myobject.someprop == something)
Checking an object's property within a valid object:
if(myobject && myobject.hasOwnProperty('your_property'))
You have a lot of possibilities in verifying objects or variables, but the all the operators work in that way in Javascript..
Here you can see more detailed ways to code your validation: How to check a not-defined variable in JavaScript