简体   繁体   中英

Check if an object literal is an 'object'

Why does the if statement not work, typeof says obj is an object.

var obj = {};
console.log(typeof obj);

if(obj === 'object') { console.log('working');}

It should be

if (typeof obj === 'object') 

for checking if the type is an object.

 var obj = {}; document.write(typeof obj + '<br>'); if (typeof obj === 'object') { document.write('working'); } 

Try:

if (typeof obj === 'object')

or

if (typeof obj == 'object')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM