check if element in object javascript code example

Example 1: check if object has method javascript

if(typeof myObj.prop2 === 'function') {
    alert("It's a function");
} else if (typeof myObj.prop2 === 'undefined') {
    alert("It's undefined");
} else {
    alert("It's neither undefined nor a function. It's a " + typeof myObj.prop2);
}

Example 2: javascript check if object

typeof yourVariable === 'object' // true if it's an object or if it's NULL.

// if you want to exclude NULL
typeof yourVariable === 'object' && yourVariable !== null