Details about the five data types in Javascript: javascript Data Types

Source: Internet
Author: User

Details about the five data types in Javascript: javascript Data Types

Undefined

Undefined. Only one undefined value exists.

Null

Only one value, null

Boolean
In javascript, it is true as long as the logical expression does not return undefined and does not return null.

Copy codeThe Code is as follows:
If (3) true
If (null) false
If (undefined) false

Number

String

The char type does not exist in javascript.

You can use single or double quotation marks to define a string.

Copy codeThe Code is as follows:
<Html>
<Head>
<Script type = "text/javascript">
// Var s = "hello ";
// Alert (typeof s); // s is a string type

Var s = new String ("hello"); // s is the object type
Alert (typeof s );
</Script>
</Head>
<Body>
</Body>
</Html>

Typeof is a unary operator used to obtain the data type of a variable.
There are five return values.Undefined, boolean, number, string, and object.

The first four are easy to understand. The last object makes the programmer unable to judge, and only returns the object in general.

In javascript, if the function does not declare the return value, undefined is returned by default.
If the return value is declared, what is actually returned.

Undefined is derived from null, so true is returned for comparison.
Alert (undefined = null); // true

Forced type conversion
In javascript, there are three types of forced conversions:

Boolean (value)

Number (value)

String (value)

Copy codeThe Code is as follows:
<Html>
<Head>
<Script type = "text/javascript">
Var num = Number (3 );
Alert (num );
Var s = "hello ";
Alert (Boolean (s ));
Var s1 = String ("hello ");
Alert (typeof s1 );
Var obj = new String ("hello"); // This Is Not A forced type conversion!
Alert (typeof obj );
</Script>
</Head>
<Body>
</Body>
</Html>

In javascript, all objects are inherited from Object objects.

It is generated in the form of new.

Some methods in js can be enumerated, and some cannot.

You can use the built-in js method to determine whether it can be enumerated.

Copy codeThe Code is as follows:
<Html>
<Head>
<Script type = "text/javascript">
Var object = new Object ();
For (var v in object ){
Console. log (v );
}
Alert (object. propertyIsEnumerable ("prototype"); // false is returned, indicating that no attribute can be enumerated. This also means that the corresponding attributes of sub-objects cannot be enumerated.
</Script>
</Head>
<Body>
</Body>
</Html>

Enumerative custom type attributes

Copy codeThe Code is as follows:
<Html>
<Head>
<Script type = "text/javascript">
Var member = function (name, age ){
This. name = name;
This. age = age;
}
Var m = new member ("liudh", 50 );
For (var v in m ){
Console. log (v );
// Name
// Age
}
Alert (m. propertyIsEnumerable ("prototype"); // false
// For (var v in window ){
// Console. log (v );
//}
</Script>
</Head>
<Body>
</Body>
</Html>

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.