2

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).

user2864740
  • 60,010
  • 15
  • 145
  • 220
Trump2016
  • 21
  • 1
  • 2

1 Answers1

0

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

Community
  • 1
  • 1
RPichioli
  • 3,245
  • 2
  • 25
  • 29