Search code examples
javascriptinstanceof

instance of in Javascript, using a variable on RHS


I am wondering if the following is allowed.

var abc2 = document.getElementById('T1').value;

alert((fruits instanceof abc2));

document.getElementById('T2').value = fruits instanceof abc2;

Can instanceof have a variable used on right hand side, instead of just string etc?


Solution

  • In Javascript, instanceof must take a constructor otherwise it will not behave correctly. If you wish to check if they come from the same constructor you could try fruits instanceof abc2.constructor.