1. Test the Data Type of the variable ---- typeof
"Undefined" ----- if this value is not defined
2. test whether the number is between the maximum and minimum ----- isFinite ()
Var result = Number. MAX_VALUE + Number. MIN_VALUE;
Alert (isFinite (result); // false
3. Determine whether a parameter is "not a value" and try to convert it to a value ------ isNaN ()
Alert (isNaN (NaN); // true
Alert (isNaN (10); // false
Alert (isNaN ("10"); // false
Alert (isNaN ("blue"); // true
Alert (isNaN (true); // false
4. convert non-numeric values to numeric values ------ Number (), parseInt (), and parseFloat ()
Var num1 = Number ("hello world"); // NaN
Var num2 = Number (""); // 0
Var num3 = Number ("0000011"); // 11
Var num4 = Number (true); // 1
Var num5 = parseInt ("0xA"); // 10 (hexadecimal number)
Var num6 = parseInt ("070"); // 56 (eight digits)
Var num7 = parseInt ("1234 blue"); // 1234
Var num8 = parseInt (""); // NaN
Var num9 = parseInt (22.5); // 22
Specify the base without 0x in hexadecimal notation
Var num10 = parseInt ("AF", 16); // 175
Var num11 = parseInt ("AF"); // NaN
Specify base number
Var num12 = parseInt ("10", 2); // 2 (parsed in binary format)
Var num13 = parseInt ("10", 8); // 8 (parsed by octal)
Var num14 = parseInt ("10", 10); // 10 (parsed in decimal format)
Var num15 = parseInt ("10", 16); // 16 (resolution in hexadecimal format)
Var num16 = parseFloat ("1234 blue"); // 1234 integer
Var num17 = parseFloat ("0xA"); // 0
Var num18 = parseFloat ("22.5"); // 22.5
Var num19 = parseFloat ("22.34.5"); // 22.34
Var num20 = parseFloat ("0980.5"); // 980.5
Var num21 = parseFloat ("3.125e7"); // 31250000
5. Check the array ------ instanceof isArray ()
If (value instanceof Array ){
// Assume that the single global execution environment ECMAScript3
}
If (Array. isArray (value )){
// ECMAScript5
}
6. insert and delete arrays ------ push (), pop (), shift (), unshift ()
Var array = new Array ();
Push (): add elements to the end of the array and return the length of the new array.
Var pushLength = array. push ("value1", "value2 ");
Alert (pushLength); // 2
Pop (): removes the last entry of the array and returns the removed entry.
Var item = array. pop ();
Alert (item); // value2
Unshift (): adds an element to the array header and returns the length of the new array.
Var unshiftLength = array. unshift ("value3 ");
Alert (unshiftLength); // 2
Shift (): remove the first entry of the array and return the removed entry.
Var itemShift = array. shift ();
Alert (itemShift); // value1
7. Operations on Arrays-delete, insert, and replace ------- splice ()
The main purpose is to insert an entry into the middle of the array.
Insert: you can insert any number of items to a specified position. Three parameters are required: Start position, 0 (number of items to be deleted), and items to be inserted.
Splice (2, 0, "red", "blue") // insert from position 2 of the array
Delete: You can delete any number of items. You only need to provide two parameters: the location of the first item to be deleted and the number of items to be deleted.
Splice () // Delete two items from array position 2
Replace: insert any number of items to the specified position of the array and delete any number of items at the same time. You only need to provide three parameters: Start position, number of items to be deleted, and number of items to be inserted.
Splice (, "red", "blue"); // Delete from array position 2, delete two items, and then insert the string "red" from position 2 ", "blue"
8. reorder of arrays ------ reverse (), sort ()
Var array = [0, 1, 5, 10, 15];
Sort (): sort strings in order.
Array. sort ();
Alert (array); // 0, 1, 10, 15, 5
Reverse (): reverse the array order
Array. reverse ();
Alert (array); // 5, 15, 10, 1, 0
Practical sort in ascending order: pass a comparison function as a parameter for the sort () function
Function compare (value1, value2) {// sort in ascending order
If (value1 <value2 ){
Return-1; // ascending/descending order control
} Else if (value1> value2 ){
Return 1; // ascending/descending order control
} Else {
Return 0;
}
}
Array. sort (compare );
Alert (array); // 0, 1, 5, 10, 15
Simplified compare Functions
Function simpleCompare (value1, value2 ){
Return value2-value1;
}
9. truncate a new array from the array. This operation will not affect the original array ------ slice ()
Var array = [1, 2, 3, 4, 5, 6, 7, 8, 9];
Var array1 = array. slice (3); // 4,5, 6,7, 8,9
Var array2 = array. slice (2, 4); // 3, 4
10. Array Construction ------ concat ()
Var array = [1, 2, 3];
Var array1 = array. concat (); // array1:, 5
Var array2 = array. concat ([, 6]); // array2 :,
Var array3 = array. concat (, [, 8]); // array3 :,
11. Location Method ------ indexOf (), lastIndexOf ()
IndexOf (param1, param2): param1: Index of the start position of the item to be searched (whether the array contains secondary elements) and param2 (optional)
Var array = [1, 2, 3, 4, 5, 6, 7, 8, 9];
Alert (array. indexOf (5); // 4
Alert (array. indexOf (5, 5); // If-1 is not found,-1 is returned.
12. array Iteration Methods: ------ every (), filter (), forEach (), map (), some () IE9 +, Firefox2 +, Safari3 +, Opera9.5 +, Chorme
Every (): run the given function for each item in the array. If this function returns true for each item, true is returned;
Determine that all elements in array A are in array B:
Var A = [1, 2, 3, 4, 5, 6, 7, 8, 9];
Var B = [1, 2, 5, 6, 6, 8];
Var boo = B. every (function (item, index, array ){
If (A. indexOf (item )! =-1 ){
Return true;
}
});
Alert (boo); // true
Var C = [1, 2, 5, 6, 6, 8];
Var booc = C. every (function (item, index, array ){
If (A. indexOf (item )! =-1 ){
Return true;
}
});
Alert (booc); // false
Filter (): If you run a given function for each item in the array, returning this function returns an array consisting of true items.
ForEach (): runs the given function for each item in the array without returning a value.
Retrieve the elements in array B that appear (not) in array:
Var A = [1, 2, 3, 4, 5, 6, 7, 8, 9];
Var B = [1, 2, 5, 6, 0, 8];
Var arr = new Array ();
B. forEach (function (item, index, array ){
If (A. indexOf (item )! =-1 ){
Arr [arr. length] = item;
}
});
Alert (arr); // 1, 2, 5, 6, 8
Map (): runs a given function for each item in the array and returns an array composed of the results of each function call.
Some (): run the given function for each item in the array. If this function returns true for any item, true is returned;
13. Zoom out method: ------- reduce (), reduceRight () IE9 +, Firefox3 +, safari 4 +, Opera 10.5, chrome
Iterate each item of the array and construct a final return value. The function receives four parameters: the previous value, the current value, the index of the item, and the array object.
Any value returned by this function will be automatically passed to the next item as the first parameter. The first iteration occurs in the second item of the array, so the first parameter is the first item of the array.
Var array = [1, 2, 3, 4, 5, 6, 7, 8, 9];
Var sum = array. reduce (function (pre, cur, index, array ){
Return pre + cur;
})
Alert (sum); // 45
ReduceRight () only performs reverse traversal and the result remains unchanged.
14. Obtain the Date and time in milliseconds when calling the method ------ Date. now ();
15. Date formatting method:
ToDateString (): display the day of the week, month, day, and year in a specific format.
ToTimeString (): displays the time, minute, second, and time zone in a specific format.
ToLocaleDateString (): displays the day of the week, month, day, and year in the region-specific format.
ToLocaleTimeString (): displays the hour, minute, and second in the region-specific format.
ToUTCString (): display the complete UTC date in a specific implementation format.
16. date/time component method:
GetTime () ------ return the number of milliseconds that indicate the date; same as the value returned by the valueOf () method
17. As a value function: compare a parameter of an object, which can be sorted by parameters (Object Attributes.
Function createComparisonFunction (propertyName ){
Return function (object1, object2 ){
Var value1 = object1 [propertyName];
Var value2 = obejct2 [propertyName];
If (value1 <value2 ){
Return-1;
} Else if (value1> value2 ){
Return 1;
} Else {
Return 0;
}
}
}
Var data = [{name: "zhang", age: 20}, {name: "li", age: 25}];
Data. sort (createComparisonFunction (name ));
Alert (data [0]. name); // "li"
Data. sort (createComparisonFunction (age ));
Alert (data [0]. name); // "zhang"
18. Internal properties of the function: ------ arguments. callee ()
Factorial calculation:
Function factorial (max ){
If (max <= 1 ){
Return 1;
} Else {
Return max * arguments. callee (max-1 );
}
}
Alert (factorial (5); // 120
Factorial calculation within a certain value range:
Function betweenFactorial (max, min ){
Return factorial (max)/factorial (min-1 );
}
Alert (betweenFactorial (5, 4); // 20
19. built-in attributes and methods of the function: ------- apply ()
Select the Maximum/minimum values in the array.
Var data = [,];
Var maxValue = Math. max. apply (Math, data );
Alert (maxValue); // 56
Var minValue = Math. min. apply (Math, data );
Alert (minValue); // 1