07-javascript common built-in objects

Source: Internet
Author: User

JavaScript common built-in objects 1. How array Array1.1 arrays are created
// create an array directly var colors = [' Red ', ' blue ', ' green 'typeof(colors)); // using constructors to create arrays with the New keyword var New Array (); Console.log (Colors2,typeof(colors2));

1.2 Assigning values to arrays
var arr = []; // The subscript of the JS array is incremented from 0, starting with Python. // add elements to the array arr[0] = ' x'; arr[1] = 3; arr[3] = [1, 2, 3];console.log (arr); // iterating through an array, printing an element  for inch arr) {    console.log (arr[i]);}

1.3 Common methods of arrays

1-Merging of arrays concat ()

// 1-Merging of arrays concat () var f1 = [' Apple ', ' banana ']; var f2 = [' orange ', ' pear ']; var fruits = f1.concat (f2); Console.log (fruits);

2- Join () joins the elements in the array using the specified string, which forms a new string

// 2-join () joins the elements in the array using the specified string, which forms a new string var fruits = ["apple", "banana", "orange", "Pear"];console.log (Fruits.join (' * * * '));

3-Convert Array to string toString ()

// 3-Convert array to String toString () var fruits = ["apple", "banana", "orange", "Pear"];console.log (Fruits.tostring ());

4-slice (Start,end); Returns a section of an array, left-closed, right-open

// 4-slice (start,end); Returns the paragraph of an array, left-closed (Gu Tou regardless of tail) var fruits = ["apple", "banana", "orange", "Pear"];console.log (Fruits.slice (2,3));

5-pop to move the last element of the divisor group

// 5-pop to move the last element of the divisor group var fruits = ["apple", "banana", "orange", "Pear"];fruits.pop (); Console.log (fruits);

6-push () Adds an element to the array at the end

// 6-push () adds an element to the array at the end of the var fruits = ["apple", "banana", "orange", "Pear"];fruits.push (' grape '); Console.log (fruits) ;

7-reverse () flipping an array

// 7-reverse () flipping an array var fruits = ["apple", "banana", "orange", "Pear"];console.log (Fruits.reverse ());

8-sort sorting an array of arrays

// 8-sort sorting an array of arrays var fruits = ["apple", "pear", "banana", "Orange"];fruits.sort (); Console.log (fruits);

9-Determine if an array: IsArray ()

// 9-Determine if an array: IsArray () var fruits = ["apple", "pear", "banana", "Orange"];console.log (Array.isarray (fruits));

2. String String2.1 string Common methods

2.2 Common Method Demos

1-chartat () returns the character of the position of the specified index, starting at 0 increments

// 1-chartat () character that returns the position of the specified index var str = ' I love to eat fish. '; Console.log (Str.charat (0)); Console.log (Str.charat (1)); Console.log (Str.charat (2) );
Console.log (Str.charat (3)), Console.log (Str.charat (4)), Console.log (Str.charat (5));

2-concat returns a String value that represents the concatenation of two or more strings

// 2-concat Returns a String value that represents the concatenation of two or more strings var str1 = ' ban ', str2 = ' ana '; Console.log (Str1.concat (str2));

3-replace (b) replaces string A with string b

// 3-replace (b) replaces string A with string B var str = ' I love to eat fish. ' ; Console.log (str.replace (' fish ', ' meat '));

4-indexof () looks for the subscript of a character, and returns 1 if it finds the index of the first occurrence of the returned string. Same as Seach () method usage

// 4-indexof () finds the subscript of a character and returns 1 if it finds a subscript for the returned string. As with Seach () method usage var str = ' I love to eat fish. ' ; Console.log (Str.indexof (' I ')), Console.log (Str.indexof (' o ')); Console.log (Str.indexof ( ' W '));

5-slice (Start,end) left close right open (Gu Tou disregard tail) split string

// 5-slice (start,end) left closed right open split string var str = ' I love to eat fish. ' ; Console.log (Str.slice (2,6));

6-split (' A ', 1) splits the string with string A and returns a new array. If the second argument is not written, the entire array is returned, and if the number is defined, the maximum length of the array is returned

// 6-split (' A ', 1) splits the string with string A and returns a new array. If the second argument is not written, it returns the entire array, and if the number is defined, the maximum length of the array is returned var str = ' I love to eat fish. '  = Str.split ("); Console.log (' arr1 '= Str.split (', 2); Console.log (' arr2 ', ARR2);

7-substr (start,end) left closed right open

// 7-substr (statr,end) left closed right open var str = ' I love to eat fish. ' ; Console.log (Str.substr (0,7));

8-tolowercase () Turn lowercase

// 8-tolowercase () Turn lowercase var str = ' I love to eat fish. ' ; Console.log (' str: ', str); Console.log (' lower: ', str.tolowercase ());

9-touppercase () Turn capital

// 9-touppercase () turn capital var str = ' I love to eat fish. ' ; Console.log (' str: ', str); Console.log (' Upper: ', Str.touppercase ());

10-Convert Number type to string type

// 10-Convert number type to String type var num = 1314.1314; Console.log (num.tostring (),typeof(Num.tostring ())); // rounded Console.log (num.tofixed (),typeof(Num.tofixed ()));

3. Creation of date objects Date3.1 date objects

Create Date object only constructor one way, use the New keyword

// a Date object was created var New Date ();
3.2 Common methods for date objects

//Create a Date objectvarMyDate =NewDate ();//Get Date (1-31)Console.log (' GetDate (): ', Mydate.getdate ());//Get Month (0-11)Console.log (' GetMonth (): ', Mydate.getmonth ());//get four-bit yearsConsole.log (' getFullYear (): ', Mydate.getfullyear ());//get the day of the Week (0-6)Console.log (' GetDay (): ', Mydate.getday ());//Get Hours (0-23)Console.log (' GetHours (): ', Mydate.gethours ());//Get minutes (0-59)Console.log (' getminutes (): ', Mydate.getminutes ());//gets the number of seconds (0-59)Console.log (' getseconds (): ', Mydate.getseconds ());

4. Built-in object Math4.1math common methods

4.2Math Common Methods Demo

1-math.floor () Down rounding, ' floor function '

// 1-math.floor () down rounding, ' floor function ' var x = 1314.1314; var y = math.floor (x); Console.log (y);

2-math.ceil () rounding up, ' ceiling function '

// 2-math.ceil () rounding up, ' ceiling function ' var x = 1314.1314; var y = math.ceil (x); Console.log (y);

3-Calculate the maximum and minimum values for two numbers

// 3-Calculate the maximum and minimum values for two numbers var x = N, y =console.log (' min: ', Math.min (x, y)), Console.log (' Max: ', Math.max (x, y)) ;

4-Random number Math.random ()

// 4-Random number math.random () var rannum = math.random (); Console.log (rannum); [ Random number between 0,1]//  Min-max: Min+math.random () * (max-min)//  100-200 random number Console.log (100+rannum* (200-100));

07-javascript common built-in objects

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.