Basic data types in JavaScript and several methods of type detection __javascript

Source: Internet
Author: User
Tags object object

There are 6 basic data types in 1.JS, and all operations in JS are based on these five basic types.
(1) Object

Object type

(2) Number

Number Type

(3) String

String type

(4) NULL

(5) underfined

(6) Boolean

Boolean type: TRUE or False

I) data type conversion in JS (not in strict mode)

"==12"//true in non-strict mode, strings can be converted to numbers

True==1//True Boolean value will attempt to convert to 0 or 1 on both sides of the equals sign

"1" ==true//true

null==underfined//true

New Object () ==new object ()//true

Nan==nan//false

New

II) data type conversion in JS (not in strict mode)

The equal sign in the above is not valid

Note: In particular, if you are a string or number in the base type, if necessary, you can convert string or number to object type, which is not persisted.
For example: Var x= "Hello"; Alert (a.length)//5

Implicit conversions in data types supplement: "32" +32//Convert to String

                     "32"-32//Convert to Number
The type detection method in JS

(1) Type detection through typeof

Let's take a look at a few examples:

typeof 100-->number

typeof "Hello"-->string

typeof True-–> Boolean

typeof Number-->function

typeof New Object () ——->object

typeof Object ——->function

typeof Null--–>object

typeof underfined--–>underfined

Summary: If the right side is the basic type, then typeof will try to get the most basic type, such as number,string and so on, if it is the function name, then return functions, where object,number,string, and so on can be regarded as function names, If the right side is a basic object, return object (the return is lowercase oh).

Note: We found typeof null, the result returned object, this is a very early bug, has been used so far

Scope of trial: If you judge a type by typeof, it applies to determining the base type, or to determining whether it is a function.

2.instanceof

Again, for example, Ming:

[1,2] instanceof array--> true

"1,2" instanceof Array ——->false

Summary: instanceof will look up along the prototype chain, return TRUE if the object on the left side is on the prototype chain, and note that only the object types used to determine the extension (non number,string, etc.)

For example: 1 instanceof number-–> false

"Hell"  instanceof String  ------>string

Add: The right side of the instanceof must be a function, or a constructor, if not it will be reported

Wrong, detection is the object on the left of the prototype chain, whether there is the right function of the prototype.

3.object.prototype.tostring

By using the Tosting method on the object prototype, you can also judge the type, and we'll give you an example:

Object.prototype.toString.apply ([])-->[object Object]

Object.prototype.toString.apply (function () {})-->[object function]

Object.prototype.toString.apply (number) ——->[object Function]

Object.prototype.toString.apply (String)--–>[object Function]

Object.prototype.toString.apply (NULL) ——— –>[object NULL]

Object.prototype.toString.apply (undefined) –>[object undefined]

Usage type: Native object and basic type

In addition, there are
(4) constructor

(5) Duck type, etc.

Related Article

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.