1let myObject = {
2 firstname: 'harry',
3 lastname: 'potter'
4}
5//check the typeof if, boolean, object, string etc...
6console.log(typeof myObject);
7if(typeof myObject === 'object') {
8 console.log('this is object');
9}
1obj = {
2 "data": 123
3}
4arr = [
5 "data",
6 123
7]
8
9function obj_or_arr(val) {
10 if (typeof val === "object") { // return if is not array or object
11 try {
12 for(x of val) // is no errors happens here is an array
13 break;
14 return "array";
15 } catch {
16 return "object"; // if there was an error is an object
17 }
18 } else return false;
19}
20
21console.log(obj_or_arr(obj)) // object
22console.log(obj_or_arr(arr)) // array
23console.log(obj_or_arr(123)) // false
24console.log(obj_or_arr("hello world")) // false
25console.log(obj_or_arr(true)) // false
26console.log(obj_or_arr(false)) // false
27
1//checks if is object, null val returns false
2function isObject(val) {
3 if (val === null) { return false;}
4 return ( (typeof val === 'function') || (typeof val === 'object') );
5}
6var person = {"name":"Boby Snark"};
7isObject(person);//true
1typeof yourVariable === 'object' // true if it's an object or if it's NULL.
2
3// if you want to exclude NULL
4typeof yourVariable === 'object' && yourVariable !== null