check something in object js code example
Example 1: javascript check if object is object
function isObject(element) {
return Object.getPrototypeOf(element) === Object.getPrototypeOf(new Object());
}
Example 2: javascript check if is object
obj = {
"data": 123
}
arr = [
"data",
123
]
function obj_or_arr(val) {
if (typeof val === "object") {
try {
for(x of val)
break;
return "array";
} catch {
return "object";
}
} else return false;
}
console.log(obj_or_arr(obj))
console.log(obj_or_arr(arr))
console.log(obj_or_arr(123))
console.log(obj_or_arr("hello world"))
console.log(obj_or_arr(true))
console.log(obj_or_arr(false))