JavaScript basic Data type

Source: Internet
Author: User

String Object

Creating objects
var S1 = new String ("Haha xiaofan")
var s2 = "HAha2"
String object property length, traversing string
document.write (S1.length)
for (var i in S1) {
document.write (i);
document.write (S1[i]);
}
Uppercase and lowercase conversions
document.write (S2.touppercase ());
document.write (S2.tolowercase ());
Gets the specified character
document.write (S1.charat ("3"));
document.write (S1.charat (3));
Query string
S1= "FFFXXXHHH1HFFH1";
document.write (S1.search ("F")); Returns the subscript of the character that matches to the first one
document.write (S1.match ("H1") [0]); The return is an array, here takes the first one
Replace replacement
document.write (S1.replace ("F", "fxh")); The original string does not change
Split split
document.write (S1.split ("X"));
Concat connection
document.write (S1.concat ("Fanxuanhui"));
SUBSTR Intercept string
document.write (S1.SUBSTR); Fetch 2 from the 1th index position
Substring
document.write (s1.substring (1,3)); Starting at index 1 fetch to index 3


Array arrays

Create an array
var arr = [n/a];
var arr1 = new Array (1,2,3,4, "a"); //
var arr2 = new Array (3); Declares an array that can put 4 objects
Length method
document.write (arr.length);
document.write (arr1.length);
The Join method. The join in JS is the method of the array. A join in Python is a method of string that connects the values in the list
var ret = ["haha", "fxh"].join ("----"); haha----fxh
Reverse reversal
var arr = [1,5,32,10];
document.write (Arr.reverse ());
Sort sorts. As a string after the comparison of the sort is asii code.
document.write (Arr.sort ());
Sort by number
function Mysort (A, b) {
if (a > B) {
return 1;
}else if (a < b) {
return-1;
}else{
return 0;
}
}
function Mysort (A, b) {return a-A;}
document.write (Arr.sort (Mysort));
Two-dimensional array: array of nested arrays
var arr2 = [[1,2,3],[3,4,5]];


Date Object

Create a Date Object
var date_obj = new Date (); Current time
document.write (Date_obj.tolocalestring ());
var date_obj1 = new Date ("2017/11/30 11:20");
var date_obj1 = new Date (5000); Unix System Birth Time +5000ms
Doucment.watch (Date_obj.toutcstring ()); World Standard Time

Date_obj.getfullyear (); Years
Date_obj.getmonth (); Month 0-11
Date_obj.getdate (); Day
Date_obj.getday (); Day of the week 0-6
Date_obj.getminutes ();

JavaScript basic 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.