2

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');}
Irfan
  • 5,070
  • 1
  • 28
  • 32
Mergim Ujkani
  • 309
  • 1
  • 2
  • 13
  • Possible duplicate of [Check if a variable is an object in javascript](http://stackoverflow.com/questions/8511281/check-if-a-variable-is-an-object-in-javascript) – ROMANIA_engineer Dec 21 '15 at 09:14

2 Answers2

4

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');
}
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
3

Try:

if (typeof obj === 'object')

or

if (typeof obj == 'object')
rharvey
  • 1,987
  • 1
  • 28
  • 23