JavaScript built-in objects (arrays, strings, dates)

Source: Internet
Author: User
Tags set time square root

JavaScript is an object-based language that consists of different objects, composed of programming languages.

Array objects, numeric objects, string objects, date objects, math objects, regular expressions, Dom objects, BOM objects, event objects

Common methods for array objects:

Length: Gets the number of elements in the array

Concat (): Merge array to generate a new array

1 <script type= "Text/javascript" >2         var arr1 = [' eat ', ' sleep ']; 3         var arr2 = [' Drink water ', ' scratching your heads ']; 4         Console.log (Arr1.concat (ARR2)); 5 </script>

Join (): Concatenate elements of an array into a string

1 <script type= "Text/javascript" >2     var arr1 = [' eat ', ' sleep ']; 3     var arr2 = [' Drink water ', ' scratching your heads ']; 4     Console.log (Arr1.concat (ARR2). Join ('-')); 5 </script>

Push (): Adds an element to the end of the array. Number of return arrays

1 <script type= "Text/javascript" >2     var arr1 = [' eat ', ' sleep ']; 3     var arr2 = [' Drink water ', ' scratching your heads ']; 4     var arr3 = arr1.concat (arr2); 5     Console.log (Arr3.push (' Listen to Song ')); 6     document.write (ARR3); 7 </script>

Pop (): bar Delete the last element of the array

1 <script type= "Text/javascript" >2     var arr1 = [' eat ', ' sleep ']; 3     var arr2 = [' Drink water ', ' scratching your heads ']; 4     var arr3 = arr1.concat (arr2); 5     Console.log (Arr3.pop ());    // scratching    arr3.pop ()6     document.write (ARR3);        // eat, sleep, drink . 7 </script>

Shift (): Delete the first element

Unshift (): Adding elements to the beginning of the array

reverse (): Reverses the order of the array. Hint: (very useful for a function, but also a face question.) Array flipping, also in PHP, the implementation of the Chinese string flashback can be used, using Preg_match_all ('/./u ', $str, $result), with implode use Array_reverse ())

1 <script type= "Text/javascript" >2     var str1 = ' I love China '; 3     var result = Str1.match (/./g); 4     result = result.reverse (); 5     Console.log (result); 6 </script>

Slice (start,end): Intercepting elements of an array

Splice (startindex,deletecount,values): Deletes an array of elements.

@param the index position at the beginning of the StartIndex

@param deletecount The number of deleted elements

@param values are optional, if there is a parameter 3, indicating that the value of parameter 3 is used instead of the deleted content

What to @return Delete

1 <script type= "Text/javascript" >2     var arr1 = [' eat ', ' sleep ', ' watch movie ']; 3     Arr1.splice (0, 2, ' bathing '); 4     Console.log (arr1);    // Take a shower, watch a movie 5 </script>

ToString (): Converts an array to a string

1 <script type= "Text/javascript" >2     var arr1 = [' eat ', ' sleep ', ' watch movie ']; 3     var result = arr1.tostring (); 4     Console.log (result);    // eat, sleep, watch a movie 5 </script>
Common methods for string objects:

IndexOf (): Gets the position of the first occurrence of a character in a string

1 <script type= "Text/javascript" >2     var str1 = ' ABCDEFG '; 3     var index = str1.indexof (' d '); 4     Console.log (index);        // 3        array subscript starting with 0 5 </script>

LastIndexOf (): Reverse ibid.

Split (): Splits a string into an array by delimiter

1 <script type= "Text/javascript" >2     var str1 = ' abcd,ef,g '; 3     var arr1 = Str1.split (', '); 4     Console.log (Arr1[0]);    // ABCD 5 </script>

Replace (A, B): Substituting some characters in a string for another character, or replacing a substring that matches a regular expression

1<script type= "Text/javascript" >2     varstr1 = ' abcd,efg123 ';3     varresult = Str1.replace (', EFG ', '-');4Console.log (result);//abcd-5     varReg =/\d+/;6     varresult = Str1.replace (Reg, '-');//Use regular replace7Console.log (result);//abcd,efg-8</script>

CharAt (Index): finds the character at index

1 <script type= "Text/javascript" >2     var str1 = ' abcd,efg123 '; 3     var result = Str1.charat (ten); 4     Console.log (result);    // 3 5 </script>

Little exercise: Find random three-digit digits, 10 bits, hundred cubes and equals it itself

1<script type= "Text/javascript" >2      for(vari = 100; I <= 999; i++) {3 4         varstr =String (i);5 6         varGE = parseint (str.charat (2));//Digit7         varShi = parseint (Str.charat (1));//10 Guests8         varBai = parseint (Str.charat (0));//Hundred9 Ten         if(Ge*ge*ge+shi*shi*shi+bai*bai*bai = =i) { Onedocument.write (i+ ' ); A         } -     } -</script>

Slice (start,end): Intercept from start and intercept to end position.

SUBSTR (start,length): Intercept from start position, length

toLowerCase (): Converts a string to lowercase. toUpperCase () conversely//str.tolowercase ()

Common methods of Mathematical objects://math objects

Math.Abs (): Calculate absolute value

Math.ceil (): Rounding up//ignoring decimals, rounding up

Math.floor (): Ditto and vice versa

1 <script type= "Text/javascript" >2     var str1 = 1.9; 3     var result = Math.floor (str1); 4     Console.log (result);    // 1 5 </script>

Math.Round (): Rounding Up rounding

Math.max (): Calculates the maximum value

Math.min (): Calculates the minimum value

1 <script type= "Text/javascript" >2     var result1 = Math.max (1,5,8,6,7,9,2); 3     var result2 = Math.max (1,5,8,6,7,9,2); 4     Console.log (RESULT1);    // 9 5     Console.log (RESULT2);    // 1 6 </script>

Math.pow (x, y): Gets x's y-square

MATH.SQRT (num): Gets the square root of num

Math.random (): Get a random number between 0-1

1 <script type= "Text/javascript" >2     var str = math.random () * Ten; 3     Console.log (Math.Round (str)); 4 </script>
Common methods for Date objects://new Date Object

Get the month day, time division seconds:

getFullYear, GetMonth (), GetDate (), getHours (), getminutes (), getseconds ()

1 <script type= "Text/javascript" >2     varnew  Date (); 3     var month = Odate.getmonth () +1;    // number of months from 0-11, so to +1 4     document.write (' Beijing Time: ' +odate.getfullyear () + '-' +month+ '-' +odate.getdate ()); 5 </script>

GetDay (): Gets the number of weeks between//0-6, 0 for Sunday

Getmilliseconds (): Gets the number of milliseconds after the time, 1000 milliseconds for 1 seconds

GetTime (): Gets the current timestamp (the timestamp in milliseconds)

1 <script type= "Text/javascript" >2     varnew Date ("2017-4-26 16:29:59:688 ")    // custom Time 3     document.write (Time.gettime ())    // 1493195399688 4 </script>

toLocaleString (): Convert Date object to cost time format

1 <script type= "Text/javascript" >2     varnew  Date ()3     document.write (time.tolocalestring ())4 </script>

Set Time:

setFullYear () //year

Setmonth ()//month

SetDate () //day

1 <script type= "Text/javascript" >2     varnew  Date (); 3     Time.setfullyear (); 4     document.write (' Last year's Today is: Week ' +time.getday ())    // last year's Today is: Week 25 </script>

JavaScript built-in objects (arrays, strings, dates)

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.