Js_ data Type null,undefined,date (), Typeconvert:

Source: Internet
Author: User

Today, the main thing to learn is the data type aspect-the more learning more feel JS this language, really is amazing,very amazing, summed up under

1. First to compare null with undefined:

The NULL:JS keyword, which represents an object, but is empty. Because it is an object, typeof (NULL) returns object,:number-->0,string--> "null" when used under primitive type context, Bool-->false

Undefined: Not the JS keyword, but the global properties of the Window object, (which can be accessed with window.undefined), typeof (undefined) returns undefined, when using undeclared variables in JS Or declare a variable without assigning a value, its value is undefined,:number-->nan,string--> "undefined" when used under primitive type context, Bool-->false

Ps:null==undefined returns True, but null===undefined returns false because typeof () is not the same.

2.Date ()

Date represents the time object, and the way to get the object is: Var t=new Date ();

1 var New Date (); 2 var New Date (one, one, and all); 3 alert (d.tolocalestring ()); // datetime 2015/12/27 PM 12:41:11 4 alert (d2.tolocaledatestring ()); // Date 2015/12/25 Note: Month starts from 0, so 11 is December

3.Error objects

Pre-defined Error object in JS: Evalerror,typeerror,referenceerror,rangeerror,syntaxerror,urierror
var e = new TypeError (); alert (typeof (e)); // Object

Let's get to the bottom.

4.Regular Expression

Regular Expression object, for string matching

var patt1=New RegExp ("E");d ocument.write (patt1.test (/// string contains ' E ', true 

5. Type Conversion Summary

① here to learn a very powerful point of knowledge, that is, for the primitive type, that is, the NUMBER,STRING,BOOLEAN,JS interpreter will have a encapsulated function to encapsulate it into the object type: Number,string,boolean, This encapsulation is automatically implemented when objects need to be used in context: for example:

1 var s1 = "Hello World"; 2 var s2 = s1.substring (2, s1.length); // object calls the method, property of the string 3 alert (S2);
A call to the following object is equivalent:
5 var New String ("Hello World"); 6 var s2 = s1.substring (2, s1.length); 7 alert (S2);

This encapsulation is temporary, that is, after using the object's characteristics, the object does not exist, only string, of course, the most commonly used is a string, although Number,bool has this feature.

Moreover, this conversion is reversible!wow,cool! Isn't it?

1 var New String ("Hello World"); 2 var s2 = s1 + ", wonderful!"; // the S1 object is first converted to primitive string value, and then the character is spliced 3 alert (S2); // Hello world,wonderful!

The primitive value of ②number,string,bool can be converted to its object form by object ():

1 varS3 = "Hello";2Alerttypeof(S3));//string3S3 =Object (S3);4Alerttypeof(S3));//Object5 6 varB1 =true;7Alerttypeof(B1));//Boolean8B1 =Object (B1);9Alerttypeof(B1));//Object

③ when using object in a bool scenario, as long as object is not NULL, its value is true:

1 //internal value is False,but object convert to true!2 varT1 =NewBoolean (false);3 vart2 =NewNumber (0);4 varT3 =NewString ("");5 varT4 =NewArray ();6 7 if(false|| 0 | | "") {8Alert ("Sorry,you can ' t see me ...");9 }Ten if(T1 && T2 && T3 &&T4) { OneAlert ("Yes, object is true!")); A}


First come here, feel, strong!

Js_ data Type null,undefined,date (), Typeconvert:

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.