Instance of and type of :- JavaScript



  • The instanceof operator returns a boolean value.
  • The typeof operator return string value.
  • The instanceof operator is used to check the type of an object at run time.
  • The typeof operator is used to get the data type of its operand.
Code:-

  1. <!DOCTYPE html> 
  2. <html> 
  3.  
  4. <head> 
  5.  <meta name="viewport" content="width=device-width, initial-scale=1"> 
  6.  
  7.  <script> 
  8.   var ar = ["1", "2", "3"]; 
  9.  
  10.   function fun1() 
  11.   { 
  12.    alert(typeof ar); 
  13.   } 
  14.  
  15.   function fun2() 
  16.   { 
  17.    var br = ["1", "2", "3"]; 
  18.    var st = "Merin jojeriojdfoifdsg"; 
  19.  
  20.    alert("is String? : " + (ar instanceof Array).toString()); 
  21.    alert("is Array? : " + (st instanceof String).toString()); 
  22.   } 
  23.  </script> 
  24.  
  25. </head> 
  26.  
  27. <body> 
  28.  
  29.  <button onclick="fun1();">typeof</button> 
  30.  <button onclick="fun2();">instanceof</button> 
  31.  
  32. </body> 
  33.  
  34. </html>
Instance of and type of :- JavaScript Instance of and type of :- JavaScript Reviewed by Network security on November 13, 2019 Rating: 5

No comments:

Useful Information

Powered by Blogger.