Some common JavaScript API, full text hand buckle, inevitable error, welcome to point out!

1. String

1. charAt()

Returns the character of the specified index.

Parameters:

  1. index
var str = "You can't go home without buying your ticket in advance.";

console.log(str.charAt(0));  / / call
Copy the code

2. charCodeAt()

Returns the Unicode encoding of the character specified index.

Parameters:

  1. index
console.log(str.charCodeAt(1)); / / 20320
Copy the code

3. concat()

String concatenation.

Parameters:

  1. The string to concatenate
console.log(str.concat("!!!")); // If you don't buy a ticket in advance, you can't go back!!
Copy the code

4. String.fromCharCode()

Returns the character corresponding to the specified Unicode encoding.

Parameters:

  1. Unicode
console.log(String.fromCharCode(1996)); / / ߌ
Copy the code

5. indexOf()

Retrieves one string from another, returning the index of the first occurrence of the string, -1 if not found

Parameters:

  1. The string to retrieve
console.log(str.indexOf("Buy")); / / 5
Copy the code

6. lastIndexOf()

Retrieves one string from another, returning the index of the last occurrence of the string, -1 if not found

Parameters:

  1. The string to retrieve
console.log(str.lastIndexOf("No")); / / 9
Copy the code

7. match()

Finding one or more matches returns an array of matches. The contents of the array depend on whether regexp has the global flag G.

Parameters:

  1. Matching rules (regular expressions or strings)
var str2 = "Jquery 3.6.0";

console.log(str2.match("J")); // ['J', index: 0, input: 'Jquery 3.6.0', groups: undefined]

console.log(str2.match(/\d/g)); // ['3', '6', '0']

Copy the code

8. replace()

The first argument to replace the matched string can also be a normal string

Parameters:

  1. Matching rules (regular expressions or strings)
  2. The new string to be replaced by (or the return value of a callback function)
console.log(str.replace("You"."我")); // I can't go back without buying my ticket in advance
Copy the code

9. search()

Retrieves the string matching the regular expression, returns index, -1 if not found

Parameters:

  1. Matching rules (regular expressions or strings)
console.log(str.search(/ I forgot /)); // -1
Copy the code

10. slice()

Intercepts a string in the specified range (left closed and right open) and returns the intercepted new string.

Parameters:

  1. The starting index
  2. End index (not available)
console.log(str.slice(0.3)); / / call you don't
Copy the code

11. split()

Splits a string into arrays, returning arrays.

Parameters:

  1. A reference to partition
console.log(str.split("")); / / [' call ', 'you', 'no', 'ask', 'before' and 'buy' and 'ticket', ', ', 'back', 'no', 'to', 'a', ' ']
Copy the code

12. substring()

Intercepts a string in the specified range (left closed and right open) and returns the intercepted new string. The parameter cannot be negative

Parameters:

  1. The starting index
  2. End index (not available)
console.log(str.substring(0.2)); / / call you
Copy the code

13. substr()

Intercepts a string of the specified length. Returns the intercepted new string.

Parameters:

  1. The starting index
  2. The number of characters to intercept
console.log(str.substr(1.2)); / / you don't
Copy the code

14. toUpperCase()

Converts letters in a string to uppercase.

console.log(str2.toUpperCase()); / / JQUERY 3.6.0
Copy the code

15. toLowerCase()

Converts letters in a string to lowercase.

console.log(str2.toLowerCase()); / / jquery 3.6.0
Copy the code

16. length

Returns the length of the string, which is an attribute rather than a method.

console.log(str2.length); / / 12
Copy the code

Second, the array

1. length

Returns the length of the array, which is a property, not a method.

var arr = [1.2.3.4.5.6.7.8];

console.log(arr.length); / / 8
Copy the code

2. concat()

Concatenation of arrays. Returns the new array after concatenation, without changing the original array.

Parameters:

  1. The array to concatenate
console.log(arr.concat([9.10])); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Copy the code

3. push()

Adds one or more elements to the end of an array. Returns the new length of the array after the addition of elements, changing the original array.

Parameters:

  1. The element to be added
console.log(arr.push(88)); / / 9
Copy the code

4. pop()

Removing the last element at the end of the array, returning the deleted element, changes the array.

console.log(arr.pop()); / / 88
Copy the code

5. unshift()

Adding one or more elements to the beginning of an array returns the new length of the array, changing the array.

Parameters:

  1. The element to be added
console.log(arr.unshift(0)); / / 9
Copy the code

6. shift()

Removes the first element at the beginning of the array, returning the deleted element. It changes the original array.

console.log(arr.shift()); / / 0
Copy the code

7. reverse()

Reverses the order of the elements in an array. It changes the original array.

console.log(arr.reverse()); // [8, 7, 6, 5, 4, 3, 2, 1]
Copy the code

8. join()

Concatenate the elements of an array into a string, returning a string without changing the array.

Parameters:

  1. Concatenation symbol
console.log(arr.join()); / / 8,7,6,5,4,3,2,1
Copy the code

9. slice()

Intercepts elements in the specified range (left closed and right open) to form a new array, without changing the original array.

Parameters:

  1. The starting index
  2. End index (not available)
console.log(arr.slice(0.2)); // [8, 7]
Copy the code

10. splice()

Removes any element from any position in the array and inserts a new element at that position. It changes the original array. Returns an array of deleted elements.

Parameters:

  1. The starting index
  2. Number of elements to delete (if this parameter is 0, it becomes insert element here)
  3. The element to be inserted at this position
console.log(arr.splice(0.1.999)); / / [8]
Copy the code

11. indexOf()

Retrieves the first occurrence of an element in the array and returns its index, -1 if not found

Parameters:

  1. The element to find
console.log(arr.indexOf(3)); / / 6
Copy the code

12. lastIndexOf()

Retrieves the last occurrence of an element in the array and returns its index, -1 if not found

Parameters:

  1. The element to find
console.log(arr.lastIndexOf(3)); / / 6
Copy the code

13. sort()

Sorts arrays by the specified rule, by default, in ascending order by the element’s Unicode encoding. It changes the original array.

Parameters:

  1. Callback function (return value >0, two adjacent elements swap positions, otherwise not)
console.log(arr.sort()); // [1, 2, 3, 4, 5, 6, 666, 7, 999]
Copy the code

Three, date,

1. getFullyear()

Return now is that year.

console.log(mydate.getFullYear()); / / 2021
Copy the code

2. getMonth()

Returns the current month, counting from 0.

console.log(mydate.getMonth()); / / 4
Copy the code

3. getDate()

Returns the current day of the month.

console.log(mydate.getDate()); / / 5
Copy the code

4. getDay()

Return to what day of the week it is.

console.log(mydate.getDay()); / / 3
Copy the code

5. getHours()

Returns the current hour.

console.log(mydate.getHours()); / / 17
Copy the code

6. getMinutes()

Returns the current number of minutes.

console.log(mydate.getMinutes()); / / 1
Copy the code

7. getSeconds()

Returns the current number of seconds.

console.log(mydate.getSeconds()); / / 45
Copy the code

8. getMilliseconds()

Returns the current number of milliseconds.

console.log(mydate.getMilliseconds()); / / 916
Copy the code

9. getTime()

Returns the current timestamp.

console.log(mydate.getTime());  / / 1620205407916
Copy the code

Four, Numbers,

1. toFixed()

Preserves the decimal of the specified number of digits.

Parameters:

  1. Keep the number of decimal places
var num = -256.1024;

console.log(num.toFixed(2)); // -256.10
Copy the code

2. parseInt()

Integer.

Parameters:

  1. The number to round.
  2. Base (indicates that the parameter 1 is calculated as a base number, which will be converted to base 10 and rounded)
console.log(parseInt(num)); / / - 256
Copy the code

3. Number.isInteger()

Checks if it is an integer and returns a Boolean value.

Parameters:

  1. The number to judge.
console.log(Number.isInteger(num));  // false
Copy the code

4. Number.isNaN()

Returns a Boolean value to determine if it is not a number.

Parameters:

  1. The number to judge.
console.log(Number.isNaN(num)); // false
Copy the code

5. Number. MAX_VALUE and Number. MIN_VAUE

Both are attributes that represent the maximum and minimum number in JavaScript.

console.log(Number.MAX_VALUE, Number.MIN_VALUE); / / 1.7976931348623157 e+308 5 e - 324
Copy the code

5. Mathematical methods

1. Math.PI

π in mathematics. This is a property.

console.log(Math.PI); / / 3.141592653589793
Copy the code

2. Math.abs()

Take the absolute value.

console.log(Math.abs(-5.6)); / / 5.6
Copy the code

3. Math.ceil()

Round up.

console.log(Math.ceil(3.1)); / / 4
Copy the code

4. Math.floor()

I’m going to round down.

console.log(Math.floor(3.9));  / / 3
Copy the code

5. Math.round()

Round off.

console.log(Math.round(3.5));  / / 4
Copy the code

6. Math.random()

Select a random number between 0 and 1, with left on and right on.

console.log(Math.random());  / / 0.06899281638748667
Copy the code

7. Math.pow()

Take some power of some number.

Parameters:

  1. digital
  2. How many times the power
console.log(Math.pow(2.10));  / / 1024
Copy the code

8. Math.sqrt()

The square root operation, or the open root.

Parameters:

  1. The number that I took the square root of
console.log(Math.sqrt(9)); / / 3
Copy the code

Sixth, other

1. The encodeURIComponent () and decodeURIComponent ()

EncodeURIComponent: Encodes the string as a URI component.

DecodeURIComponent: Decodes the URI encoded by encodeURIComponent().

var url = "http://www.google.com";

var str = encodeURIComponent(url); 

console.log(str); // http%3A%2F%2Fwww.google.com

console.log(decodeURIComponent(str)); // http://www.google.com
Copy the code