JavaScript learns the array/boolean class/String class in-js

Source: Internet
Author: User
Tags array length first string truncated

JavaScript learning

"Arrays in JS" 1, the concept of arrays: The order in which the elements of the structure of an ordered element are sequentially stored in memory is called subscript, and the object element is looked up by subscript. 2, the declaration of the Array: ① literal declaration: var arr1 = [];js, the same array, can store many different data types: for example: var arr1 = [1, "2", true,{"name": "hahhah"},[1,2]]②new keyword declaration: var arr2 = NE W arry (parameters) >>> parameters can be a. Null: Declares one, does not have an array of the specified length, B. Length: Declares an array of a specified length, but the length of the array can be variable at any time; maximum length is 0~ (2^32-1) C. Array default n values: New Array (1, "2", ture), equivalent to [1, "2", Ture] 3. Read/write/delete of elements in array: ① Read and write: Access to elements by subscript, such as Arr[2];② additions and deletions: delete arr[n]; Deletes the n+1 value of the array, but the array length is unchanged, the corresponding position value is Undefinedarr.push (value); The array finally adds a value equivalent to arr3[ Arr3.length] = value; Arr.unshift (value), the No. 0 bit of the array inserts a value, the remaining digits are deferred, ARR.POP9 (); Deletes the last array, unlike the delete, the array length after the pop execution also decreases by one , equivalent to Arr3.length-1;arr.shift (value), delete the No. 0 bit of the array, the length will also be reduced by one; 4. Other common methods in arrays: ①join (' delimiter '); separates the array with the set delimiter, linked as a string. When the argument is null, the default is separated by commas; ②concat (); (the original array is not changed), the array is linked to the values of the multiple arrays as a new array; [1,2].concat ([3,4],[5,6]) =[1,2,3,4,5,6]; When linking, the brackets are removed at most, [1,2].concat ([1,2,[3,4]]) =[1,2,1,2,[3,4]; multi-layered brackets, in two-dimensional array form ③push (): Array last increment; Unshift (): Array starts with a number of increments " Return array new Length "Pop (): delete array last; Shift (): Delete array the first" return deleted value "" Call the above method, the original array is changed "④recerse ():" The original array was changed "array flipped, in reverse order. ⑤slice (Begin,end): "The original array will not be altered" intercepts a part of the array, becomes a new array >>> passes a parameter: The default is begin index, and when this starts, it is truncated to the last;>>> Pass two parameters: intercept from begin to end interval, left closed right (including begin, without end) >>> parameter can be negative, 1 means last; ⑥sort (function): "The original array will be altered" to sort the arrays;> >> does not specify a sort function: Arranges the;>>> incoming sort function by the ASCII value of the number: default two parameter A, B, if the return value is >0, then a>b; returns the value <0, then a<bvar arr7 = [ 7,8,10,13,1,6,99]arr7.sort (A, A, b) {return a B;//b before, a in the back (ascending order)//return b-a;//a in the front, b. in the Back (descending order)}); Console.log ( ARR7) ⑦indexof (Value,index): Returns the subscript position corresponding to the first value value in the array, and returns -1lastindexof (Value,index) If not found: Returns the subscript position corresponding to the last value value. >>> if the index parameter is not specified: The default is to query all elements of the array; If you specify the index parameter: start with the current index and query backwards; 5, two-dimensional arrays & sparse arrays (Learn):① sparse array: The array does not contain all indexes starting from 0 to length-1 (the length value is more than the actual number of elements); ② two-dimensional array: var arr = [[1,2],[3,4],[5,6]];//] three rows and two columns takes two-dimensional array elements: arr[line number [Column number];//can be traversed using nested loops 6. Reference data type & basic data type:When assigning a value to a reference data type, the address of the original variable is assigned to the new variable. Two variables, actually manipulating the same data, so modify one of the variables, the other changes; basic data type: When assigning, it is the value of the future variable, assigned to the new variable. Two variables, also belong to different memory space, modify one, do not interfere with each other. "Boolean class"var intrue = true; The simple variable var inTrue1 = new Boolean (true);//A Boolean object Number class
  • Number.min_value the minimum number that can be represented
  • Maximum number of number.max_value that can be represented
  • . toString (): Convert a number to a string equivalent to num+ ""
  • . toLocaleString (): Converts numbers into strings in the order of local formatting. Generally, three a group with commas.
  • . VALUEOF (): Returns the base numeric value of the number object
  • . toFixed (N): Converts a number to a string, retains n decimal places, rounded.
  • . Toprecision (N): Formats a number to a specified length, n= does not contain all the digits of the snack shop and
Eg:var num = 1; The simple variable var num1 = new number (2);//An object of Numbor type Console.log (NUM1); "String Class" 1. Properties: Str.length Returns the length of the string; 2. Methods:. toLowerCase () all strings are converted to lowercase;. toUpperCase () all strings are capitalized;. charAt (n) intercepts the nth character in a string;. IndexOf ("query string", index) query starts with index, The index of the first string. Not found returns-1, the same array as the indexof () method;. substring (begin,end) intercept string >>> pass a parameter: The default is begin index, and when this starts, the last one is truncated;>>> Pass two parameters: intercept from begin to end interval, left closed right (including begin, without end) >>> parameter can be negative, 1 means last; replace ("old", "new") replaces the first old in the string with the new one. The first argument can be a normal string, or it can be a regular expression (the normal string will only match the first one, and the regular expression is more specific). Split ("") divides the string into arrays by specifying the delimiter. 0

JavaScript learns the array/boolean class/String class in-js

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.