JS data type

Source: Internet
Author: User

JS Data Type classification

Base data type: Number,string,boolean,undefined,null

Complex data type: Object,array,function,regexp,date,error

Global Data type: Math

We can use TypeOf to determine the type of a variable.

But usually only 6 states are returned, respectively:

Undefined not initialized

Boolean

String

Number

Object objects or NULL

function functions

If you want to determine specifically which type of object is, use the

Object.prototype.toString.call ();

Array Object

1, the creation of the array

1 var New Array (); 2 arr[0] = 0; 3 arr[1] = 1; 4 5 6 var New Array ("BMW", "Volvo"); 7 8 9 var New Array (10);

 

Common methods:

Concat () connects two or more arrays and returns the result

Filter () returns an array of all elements that match the criteria

Find () returns an array element (first) that conforms to an incoming test (function) condition

IndexOf () returns the position of the array element

Join () Places all elements of an array into a string

Map () processes each element through a function and returns the processed array

Pop () deletes the last element of the array and returns the Delete element

Push () Adds an element to the end of the array

Reduce () evaluates the array element to a value (left to right) Reduceright ()

Reverse () Reverse element order

Sort () sorting

For example, filter () can be filtered on an array

1 var ages = [32,33,16,46]; 2 function Filterages (age) {3     return ages>=18;   4 }56 ages.filter (filterages); // [32,33,46]

For example, a join () can be used to splice () a specified stitching delimiter.

1 var arr = ["I", "Love", "You"]; 2 alert (Arr.join ("_"));       //    Output I_love_you

For example, if you need to delete the last element of an array dynamically, you can use Pop ()

1 var arr = ["I", "Love", "You"]; 2 alert (Arr.pop ());   //    Output  you3 alert (arr);         //   Output [I,love]

Another example is to ask for an array of and can use the reduce () method, and the computation speed is much faster than the loops and the like

1 vararr = [A.];2Alert (Arr.reduce (function(P1, p2, p3, p4) {3         returnp1+=P2;4}));//Output 65 6 //If you want to give an initial value, you can7 vararr = [A.];8Alert (Arr.reduce (function(P1, p2, p3, p4) {9         returnp1+=P2;Ten},5));//output 11, start adding on 5 basis

Or maybe there's a way.

1 var arr = [n/a]; 2 Alert (eval (arr.join ("+")));   // Output 6

Date Object

var New // Get current Time  Sun Sep 15:16:05 gmt+0800

If running on your server gets the time on your service

1      varDate =NewDate ();2 Console.log (date);3     //Get current date (1~31)4 Console.log (Date.getdate ());5     //Get the day of the Week (0~6)6 Console.log (Date.getday ());7     //Get Month (0~11)8 Console.log (Date.getmonth ());9     //Get yearTen Console.log (Date.getfullyear ()); One     //when acquired (0~23) A Console.log (Date.gethours ()); -     //get Points (0~59) - Console.log (Date.getminutes ()); the     //get Seconds (0~59) -Console.log (Date.getseconds ());

Results

If you +UTC in front of each method, you can get UTC time

If you change get to set, then you can set the corresponding time

There are a few more practical methods.

1     // gets the number of milliseconds from January 1, 1970 to the specified time (plus time zone) 2     Console.log (Date.gettime ()); 3     // turn the time into a string type 4     Console.log (date.tostring ()); 5     // turn the time into a local string type 6     Console.log (date.tolocalestring ()); 7     // turn the time into a utcstring type 8     Console.log (Date.toutcstring ());    

Results

GetTime can be used to calculate the time difference

So, how do you construct a date type of time?

1. Convert the number of milliseconds to date

1 var New Date (1000*60*1); 2 console.log (date);   Thu Jan 1970 08:01:00 gmt+0800 (China Standard Time)

2, the string into a Date object is mainly 2 formats

YYYY/MM/DD HH:mm:ss recommended, can omit time, omit return 00:00:00

YYYY-MM-DD HH:mm:ss omit time return 08:00:00; if not omitted, conversion in IE will fail

1     varDate =NewDate ("1993/07/08 11:11:11");2Console.log (date);//Thu Jul 1993 11:11:11 gmt+0800 (China Standard Time)3 4     varDate =NewDate ("1993/07/08");5Console.log (date);//Thu Jul 1993 00:00:00 gmt+0800 (China Standard Time)6 7     varDate =NewDate ("1993-07-08 11:11:11");8Console.log (date);//Thu Jul 1993 11:11:11 gmt+0800 (China Standard Time)9 Ten     varDate =NewDate ("1993-07-08"); OneConsole.log (date);//Thu Jul 1993 08:00:00 gmt+0800 (China Standard Time)

If you do not omit the time in IE can not be converted

3, New Date (year,month,opt_day,opt_hours,opt_minutes,opt_seconds,opt_milliseconds) with opt is can be omitted

Year (int) 4 digits

month (int) 2-digit 0~11

Opt_day (int) 2-digit 0~23

opt_hours (int) 2-digit 0~59

Opt_minutes (int) 2-digit 0~59

Opt_seconds (int) 2-digit 0~59

Opt_milliseconds (int) 0~999

1     varDate =NewDate (1993,7,8);2Console.log (date);//Sun 1993 00:00:00 gmt+0800 (China Standard Time)3 4     varDate =NewDate (1993,7,8,12);5Console.log (date);//Sun 1993 12:00:00 gmt+0800 (China Standard Time)6 7     varDate =NewDate (1993,7,8,12,12);8Console.log (date);//Sun 1993 12:12:00 gmt+0800 (China Standard Time)9 Ten     varDate =NewDate (1993,7,8,12,12,12); OneConsole.log (date);//Sun 1993 12:12:12 gmt+0800 (China Standard Time) A  -     varDate =NewDate (1993,7,8,12,12,12,12); -Console.log (date);//Sun 1993 12:12:12 gmt+0800 (China Standard Time)

JS data type

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.