JavaScript data types

Source: Internet
Author: User

A. JS has 5 basic types and 1 object types (reference types)

5 Basic types: Number\string\null\undefined\boolean

1 Types of objects: Function, Array, Date

Two. Type conversion

Skillfully type conversion with +/-

Three. = = and = = =

= =:

1. For the base type, try the type conversion and then compare!

2. For reference types, comparisons are being directed to the same object (address comparison)

= = =:

1. Return FALSE for different types

2. The same type:

Null===null

undefined===undefined

Four. Special value: NaN

Nan:not a Nnmber

This value was introduced in the original case of "returning a number but not returning a number". This situation (such as "I") in other languages will be an error, but in JS will not error, but the return value Nan. So that the process can continue to execute.

The particularity of Nan: It is not equal to any type of comparison, including its own! So to determine whether a value is Nan, you must use the global function isNaN above the Window Object!

1. For the base type, try the conversion of number type before judging

2. For an object type, the first call to its valueof attempts to convert and then judge

    var o = {        valueof:function () {            return "$";        }    }    IsNaN (o);//false

Five. Packaging objects

Some of the basic types have corresponding wrapper objects:

' str ': Wrapper object String

123: Wrapper Object number

True: Wrap Object Bealon Objects

When the base type tries to use it as an object (for example, by using its Length property to increase its properties), JS converts it to the object of the wrapper type (temporary object), but when such a visit is completed, JS destroys the temporary object!

Six. Type detection

1.typeOf: Basic type, function type

2.instanceof: operator based on the prototype chain

Obj instanceof object determines if obj has a Object.prototype object on the prototype chain (that is, if obj is an instance of object)

3.object.prototype.tostring

The previous two types of detection methods are more commonly used!

JavaScript data types

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.