How to check if a value is an object in JavaScript

1 Answer

0 votes
function isObject(val) {
   return val && typeof val === 'object' && val.constructor === Object;
}
 
const o = {
   lang: 'javascript'
};
 
console.log(isObject(o));
 
const s = 'javascript';
 
console.log(isObject(s));
 
   
     
     
/*
run:
     
true
false
     
*/

 



answered Jan 25, 2021 by avibootz
edited Apr 14, 2022 by avibootz

Related questions

2 answers 210 views
2 answers 222 views
2 answers 221 views
1 answer 156 views
...