Array, string basic syntax (Prerequisites for beginners)

Source: Internet
Author: User

Array+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ (1) Syntax: Create an array A, the literal way to create var variable name = [data 1, data n]; B, constructor creates var variable name = new Array ();//null array var variable name = new Array (len);//Create an array of size Len, len Integer, Number data type var variable name = new Array (data 1, data N) (2) Common methods in arrays (try to remember)   A: Add array elements, get array element values Arr.push (V1,v2 ...): Adds one or more elements to the end of the array         parameters: array elements to be added       return value: The length of the array     NOTE: original array change       Arr.pop (): delete one or n elements at the end of the array;      &N Bsp Return value: Deleted elements         NOTE: original array change        arr.unshift (V1,v2 ...): Add one or more elements to the head of the array   parameter Number: value to insert   return value: Array length   NOTE: Original array change       Arr.shift (): Delete array header one or n elements;  return value: deleted element   NOTE: Original Array change     b: Inverse arr.reverse (): Invert the elements of an array   Note: The original array changes     c: Search [Important] indexOf (v1): Find v1 from the array header, Find the words back index   parameters: The value to find;  return value: Index, no found return -1  lastIndexOf (v2): Find v2 from the end of the array, find the words return index   parameters: value to find   return Value: Index, not found return-1 note: If it is the same unique array element, the twoThe search results in the same subscript value;   D: sort [important] sort ([Collation function]): Sort (function (A, b) {//a==b return 0;  A<b Return-1;a in front of B//a>b return 1;a in the back of B}); Add return a-B; Returns the order array default lowercase and number, non-numeric based on ASCII numeric comparison    E:splice (i,n, array element)-----Delete or replace or add specific elements in a specific position in an array I: Insert array subscript value [  The array element before]; n:1[Delete the array element, can be n],0[remain unchanged, do not delete] Insert element [optional, you can enter multiple] if n=0, then the return value is empty, array length plus n if n! =0, the return value is the replaced array, the length is constant   string ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ (1) string [sequence of characters enclosed in double quotation marks]   A: Create string 1) literal var str = "";//' typeof str--->string---> Basic data Type 2) constructs var str=new string ("");  C: String Method 1) The character Method Charat (index)----->string A single character, gets the character charCodeAt (index)------number ASCII value of index, Gets the ASCII code for the character of Index String.fromCharCode (ASCII code)-----ASCII--->string single character, converts ASCII code to character (static method) 2) string substring search   IndexOf ("substring" [, StartIndex])   finds the first occurrence of the specified string from the head of the string, if no return -1  is found, the default starting index is 0   lastIndexOf ("substring" [, StartIndex])   The last occurrence of the string from the end of the string, if not found, return -1 3) Replace, match replace ("string to replace", "replacement string")   Parameter 1: replaced string or regular expression   parameter 2: Replacement string   return value: Replaced string   Note: The original string did not change    features: The default "substituted substring---string" can only be replaced once   If later match several times, size ignores, according to certain rule match, next week study regular   such as: Var new2=str2.replace (/adc/gi, "");//GI is global;//does not need to add "";  match will match the parameters, If the match is not on, returns null if match, then returns the array of matching results   default does not use regular, matches once  search (matching parameter)   matching parameter only one, either regular expression, or string item   Return value: The index of the first occurrence, otherwise-1; always from the head of a stringStart lookup, ignore global g  with regular also output first index;  4) intercept substring (Startindex[,endindex]) from string subscript startIndex start intercept, to EndIndex (not included) end   return value: truncated substring, no effect on the original string  substr (Startindex[,length]) starts interception from the string subscript startIndex, to length (not included) end   return value: truncated substring   Note: The original string does not change the method contains the header, does not contain the tail, but if the tail argument is omitted, it also contains the last character (if no argument is returned to the end of the object)  5) case Conversion touppercase ()   Converts all lowercase characters of a string to uppercase characters, the uppercase character remains the same    toLowerCase ()   converts all lowercase characters of a string into lowercase characters, and lowercase characters remain the same   6) Split split Splic (Separate,[howmany])   split into array according to delimiter   Note: \ Escape return value: New array  Mathematical Functions:(1) The maximum value of Math.max ([any number of values]) (2) to find the minimum math.min ([any number of values]) (3) Rounding method rounded up Math.ceil (numeric), down rounded math.floor (value); Rounding: Math.Round (  Value) (4) Other N of the M Power: Math.pow (N,M);  Take absolute value: Math.Abs (numerical value); Take square root: math.sqrt (value;)

Array, string basic syntax (Prerequisites for beginners)

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.