In JavaScript typeof and instanceof

Js difference in the instanceof and typeof

 Demo 1

   var v5=new Number("22");

   document.write(typeof v5+"<br/>"); //---->object

   document.write( v5 instanceof Number ); //---->true

document.write("<br/>"+v5);

 

result:

 

 

 

 

 

 

Tour 2:

// test string aa is not of type String

   //document.write(typeof "aa" +"<br/>"); //---->string

   // An example of test string aa is not a String

   //document.write("aa" instanceof String); //---->false

   // If you create an instance of a new way of it, then go to test two of the above

   var str3=new String("bbb");

   document.write(typeof str3 +"<br/>"); //---->Object

   // An example of test string aa is not a String

   document.write(str3 instanceof String); //---->true

Demonstration Results

 

 

 

 

Tour 3:

// test string aa is not of type String

   document.write(typeof "aa" +"<br/>"); //---->string

   // An example of test string aa is not a String

   document.write("aa" instanceof String); //---->false

   // If you create an instance of a new way of it, then go to test two of the above

   var str3=new String("bbb");

   //document.write(typeof str3 +"<br/>"); //---->Object

   // An example of test string aa is not a String

   //document.write(str3 instanceof String); //---->true

 

result

 

 

 

 

Demo 4

 

 

document.write(typeof 1 +"<br/>"); //--->number

   document.write(1 instanceof Number ); //---->false

 

/* var v5=new Number("22");

   document.write(typeof v5+"<br/>"); //---->object

   document.write( v5 instanceof Number ); //---->true

   document.write("<br/>"+v5); */

 

 

Summary: As can be seen from the above three demo for immediate thought is a certain type of variable, such as plastic surgery is considered 1, "aa" is String If typeof keywords get is in line with our ideas

如1 typeof Number--àtrue

But if 1 instanceof Numberàfalse

But if they go to a new object, then, then go call these two methods, see below

var str3=new String("bbb");

   //document.write(typeof str3 +"<br/>"); //---->Object

   // An example of test string aa is not a String

   //document.write(str3 instanceof String); //---->true

 

or

var v5=new Number("22");

   document.write(typeof v5+"<br/>"); //---->object

   document.write( v5 instanceof Number ); //---->true

That is out of our new and if we are not that type of time with new typeof get is of type Object, but instanceof that is to verify compliance with our ideas

Guess you like

Origin www.cnblogs.com/isme-zjh/p/11535472.html