JS check object property type

by Aieman Siddique

JavaScript

var data = [
{ id: 1, firstName: 'John', lastName: 'Smith', providerNumber: 'P1234', services: ['Service A', 'Service B', 'Service C'] }];

var x = data[0];

function typeOf(value) {
    var s = typeof value;
    if (s === 'object') {
        if (value) {
            if (value instanceof Array) {
                s = 'array';
            }
        } else {
            s = 'null';
        }
    }
    return s;
}


console.log(typeOf(x.services));


var y = data[0];

console.log((y.services instanceof Array));

console.log(Object.getOwnPropertyNames(y));