0

I faced this problem when I learn javascript. I tried the following code:

var arr = [1, 2, 3];
console.log(arr instanceof Array); // true
console.log(arr[0] instanceof Number); // false

var arr = [{key: 'val'}, {key1: 'val2'}];
console.log(arr[0] instanceof Object); // true

I was wondering it will log true in console, but it does not come as expected.

Please help me to understand this. Thanks in advance.

Subramanian
  • 170
  • 3
  • 12
  • 1
    the second example should return `true`, not `false` – Nick Parsons Jul 08 '20 at 05:22
  • after `instanceof` there should always be a reference type variable(object or array) and it should be declared as well . In first case it was type of number that's why it is returning false. and in second case you should declare it as `let obj = {key: 'val'}` and then `console.log(obj instanceof Object); ` , it would be returning true. – Sunny Jul 08 '20 at 05:39

0 Answers0