JavaScript basic data types and types of testing common methods summary _javascript skills

Source: Internet
Author: User
Tags object object

The examples in this article describe the basic data types in JavaScript and several methods of type detection. Share to everyone for your reference, specific as follows:

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 convert to numeric
true==1//True Boolean values will attempt to convert to 0 or 1 "1" ==true on either side of the equals sign
//true
null==underfined/ /true
New Object () ==new object ()//true
Nan==nan//false

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.

Like what:

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 base type, then TypeOf will try to get the most basic type, such as number,string and so on, and if it is the function name, then return the functions, where object,number,string, And so on can be regarded as function name, if the right is a basic object, then 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.)

Like what:

Instanceof number-–> False
"Hell" instanceof String------>string

Add: instanceof to the right must be a function, or a constructor, if not it will be an error, 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.

For more on JavaScript related content to view the site topics: "JavaScript object-oriented Tutorial", "javascript json operation tips Summary", "JavaScript switching effects and techniques summary", " JavaScript Search Algorithm Skills summary, javascript error and debugging skills Summary, JavaScript data structure and algorithm skills summary, JavaScript traversal algorithm and skills summary and JavaScript mathematical Operational Usage Summary

I hope this article will help you with JavaScript programming.

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.