Five kinds of data types in JavaScript _ basics

Source: Internet
Author: User

Undefined

Not defined. Only one value undefined

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 Code code as follows:

if (3) True
if (null) false
if (undefined) false

Number

String

The char type does not exist in JavaScript.

String definitions can be in single quotes, or in double quotes.

Copy Code code as follows:

<script type= "Text/javascript" >
var s= "Hello";
Alert (typeof s);//s is a string type

var s=new String ("Hello");//s is an object type
Alert (typeof s);
</script>
<body>
</body>


typeof is a unary operator that is used to get the data type of a variable
Its return value has five undefined,boolean,number,string and object.

The first four of them are well understood. And the last object is for the programmer to be unable to judge, only the general return of the object

In JavaScript, if a function does not declare a return value, it returns undefined by default.
If the return value is declared, then what is actually returned.

Undefined is derived from null, so returns True when comparing
alert (undefined==null);//true

Force type conversions
In JavaScript, there are three types of coercion:

Boolean (value)

Number (value)

String (value)

Copy Code code as follows:

<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 mandatory type conversion!
Alert (typeof obj);
</script>
<body>
</body>

In JavaScript, all objects inherit from Object objects.

Generated in the new way.

Some of the methods in JS can be enumerated, and some of them are not.

JS Built-in method can be judged, whether it can be enumerated.

Copy Code code as follows:

<script type= "Text/javascript" >
Var object=new object ();
For (Var v. in object) {
Console.log (v);
}
Alert (Object.propertyisenumerable ("Prototype"))//returns false indicating that there are no attributes that can be enumerated, and that the corresponding property of the child object is also not enumerable
</script>
<body>
</body>

Enumerating properties of custom types

Copy Code code as follows:

<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>
<body>
</body>

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.