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


/ Published in: JavaScript
Save to your folder(s)

Basic usage of the in and instanceOf operators.


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.