We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

1man on 11/13/07


Tagged

javascript in instanceOf


Versions (?)


Basic Usage of the 'in' and 'instanceOf' Operators


Published in: JavaScript 


Basic usage of the in and instanceOf operators.

  1. function operators(){
  2. //Use of in Operator
  3. var o = {x: 1, y: 2};//Create object
  4. var haveX = "x" in o;//true
  5. var haveY = "y" in o;//true
  6. var haveZ = "z" in o;//false
  7. var haveMethod = "toString" in o;//true (inherited property)
  8. console.log(haveX, haveY, haveZ, haveMethod);
  9.  
  10. var a = [1, 2, 3];
  11. var check1 = a instanceof Array;//true
  12. var check2 = a instanceof Object;//true (array is an object)
  13. var check3 = a instanceof RegExp;//false
  14. console.log(check1, check2, check3);
  15.  
  16. var d = new Date();
  17. var check1 = d instanceof Date;//true
  18. var check2 = d instanceof Object;//true
  19. var check3 = d instanceof Number;//false
  20. console.log(check1, check2, check3);
  21. }

Report this snippet 

You need to login to post a comment.