A detailed explanation of the difference between instanceof and typeof operators

Source: Internet
Author: User

Although the two operators are similar, in fact, the difference is very large, although it is not difficult to distinguish, but for beginners may have a little trouble, the following is a brief description of the difference between them, I hope to need a friend to help

One. instanceof operator: This operator can determine whether a variable is an instance of an object (class), and the return value is a Boolean type.   To understand its role, you must have an understanding of object-oriented: code examples are as follows: The code is as follows: Var str=new String ("Antzone");     Console.log (str instanceof String); The above code outputs True because STR is an object instance of object string.     In general, only objects created with constructors return true, or False if the array is an exception, and returns True. Two. typeof operator: This operator can return a string that describes the type of the element arithmetic, and its return value is as follows: The copy code code is: number,boolean,string,function,object,undefined First look at a generation  Code instance: codes are as follows: Var str=new String ("Antzone");   var strtwo= "Antzone";  Console.log (typeof str);     Console.log (typeof Strtwo); In the above code, the first one can output the exact type "string", and the second is "object", which is not accurate. In general, the use of typeof is a direct amount of form can return accurate results, if the use of constructors created objects will return "object", but for the array is an exception, regardless of whether the direct amount will return "object."

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.