> For the complete documentation index, see [llms.txt](https://gzqsjtu.gitbook.io/workspace/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gzqsjtu.gitbook.io/workspace/javascript/function-api.md).

# function API

Array, String, Object

## Array<br>

InPlace:push()/pop(), shift()/unshift(), **splice(),** copyWithin(), fill(), reverse(), sort()<br>

ReturnNew: concat(), slice()\
`***`**splice(),     splice(startIndex, deleteNumber, input new to here);**

```
array.splice(start[, deleteCount[, item1[, item2[, ...]]]])
```

Matching: indexOf(), lastIndexOf(), find(), findIndex(), some(), every(), join()ty

#### Empty the Array

var arr = \[1]

1. arr.splice(0, arr.length);
2. arr.length = 0;
3. arr = \[]

**traverse forEach** executes the provided callback once for each element present in the array in ascending order.

```
fruits.forEach(function (item, index, array) {
    console.log(item, index);
});
```

•Does not make a copy of the array before iterating.

```
split
var myArray = myData.split(',');
var myNewString = myArray.join(',');
myNewString;
```

&#x20;push(): add to last.

pop(): delete last and return this one.

[`toString()`](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/toString)

1. ```
   var dogNames = ["Rocket","Flash","Bella","Slugger"];
   dogNames.toString(); //Rocket,Flash,Bella,Slugger
   ```

```
var myArray = ['Manchester', 'Leeds', 'Carlisle'];
myArray.push('Cardiff');
myArray.pop();
pop() delete last one
var removedItem = myArray.pop();
```

[`unshift()`](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift)  [`shift()`](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/shift) `100% like push and pop, only on the begining`

`unshift(): add to head.`

```
myArray.unshift('Edinburgh');
var removedItem = myArray.shift();
```

```
.indexOf(element) return the index
```

Splice: delete some elements:

```
array.splice(start[, deleteCount[, item1[, item2[, ...]]]])
start position, delete number, items to add.from start
return the [] that deleted.
```

```
var removedItem = fruits.splice(pos, 1); 
```

.fill() fill all indexes.

[`.concat()`](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/concat)&#x20;

[`Array.prototype.includes()`](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/includes)

[`Array.prototype.join()`](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/join)   `concat and return a new String`

join(" + ") use " + " to concat

[`Array.prototype.slice()`](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/slice)  `get some return a new array`

```
arr.slice(begin, end);
// [begin, end)
```

[`Array.prototype.toString()`](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/toString)

[`Array.prototype.lastIndexOf()`](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf) `return the last index..`

Traverse:

[`Array.prototype.forEach()`](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach)

[`Array.prototype.map()`](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/map)

![](https://4116032991-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgnugJ0SdX2tMAwP37V%2F-Li6lCc1yK8BklAkdxk2%2F-Li6lDIjLkob_ZyGL7MN%2Fimage.png?alt=media\&token=0113e6cb-2266-4de0-9504-eb8b5d0b0148)

[`Array.prototype.reduce()`](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce)

let finalVal = oldArray.reduce((accumulator, currentValue, currentIndex, array) => { ... }), initalValue;

![](https://4116032991-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgnugJ0SdX2tMAwP37V%2F-Li6lCc1yK8BklAkdxk2%2F-Li6l_4PSuL5t8x5vTMJ%2Fimage.png?alt=media\&token=a4684f5a-dfbb-4233-bd49-4852e98dd7bc)

[`Array.prototype.some()`](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/some)  `as least one or return false;`

[`Array.prototype.filter()`](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/some)

![](https://4116032991-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LgnugJ0SdX2tMAwP37V%2F-Li6lCc1yK8BklAkdxk2%2F-Li6lNmp7bnfIEJ82q6P%2Fimage.png?alt=media\&token=3b00c2c1-bcf3-4194-ac9b-94068b49424d)

## Object

•**map** transforms the elements in the array

•Based on a function you give

•Return a copy and doesn’t modify the input array

•When the function you provide gets called, it gets called for each element with three arguments: the element itself, the index of the element, and the array itself (which is seldom useful).

•**filter** removes unwanted things from an input array.

•Based on a function you give

•Return a copy and doesn’t modify the input array

•map and filter often are used together

[`Object.assign()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign)Copies the values of all enumerable own properties from one or more source objects to a target object.

[`Object.keys()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys)Returns an array containing the names of all of the given object's **own** enumerable string properties.

[`Object.values()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values)Returns an array containing the values that correspond to all of a given object's **own** enumerable string properties.

### Reduce()

array.reduce((accumulator, currentValue, currentIndex, sourceArray) => {  })

```
arr.reduce(callback(accumulator, currentValue[, index[, array]])[, initialValue])
```

## For Loop:

for（x of Array） {//  }  arrays strings maps nodelists etc

but loop an object: we use in.

for (x in Object){ //  }

While loop  / do while loop

## String

Strings can also be created using the `String` global object directly:

```
String(thing)
new String(thing)
```

#### [Character acces](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#Character_access)s <a href="#character_access" id="character_access"></a>

&#x20;string.[`charAt()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt)

```
return 'cat'[1];
```

### [`String` instance](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#String_instances) <a href="#string_instances" id="string_instances"></a>

`String.prototype.constructor`Specifies the function that creates an object's prototype.[`String.prototype.length`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length)Reflects the length of the string.

### [`String.prototype.concat()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/concat)Combines the text of two strings and returns a new string.

### [`String.prototype.includes()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes)Determines whether one string may be found within another string.

[`String.prototype.indexOf()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf)Returns the index within the calling [`String`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) object of the first occurrence of the specified value, or -1 if not found.

[`String.prototype.lastIndexOf()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf)Returns the index within the calling [`String`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) object of the last occurrence of the specified value, or -1 if not found.

[`String.prototype.match()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match)Used to match a regular expression against a string.[`String.prototype.matchAll()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/matchAll)Returns an iterator of all matches.[`String.prototype.normalize()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize)Returns the Unicode Normalization Form of the calling string value.

[`String.prototype.repeat()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat)Returns a string consisting of the elements of the object repeated the given times.

### [`String.prototype.replace()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace)Used to find a match between a regular expression and a string, and to replace the matched substring with a new substring.

[`String.prototype.search()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/search)Executes the search for a match between a regular expression and a specified string.

### [`String.prototype.slice()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice)Extracts a section of a string and returns a new string.

```
str.slice(beginIndex[, endIndex])
```

### [`String.prototype.split()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split)Splits a [`String`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) object into an array of strings by separating the string into substrings.

```
str.split([separator[, limit]])
```

### [`String.prototype.substring()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring)Returns the characters in a string between two indexes into the string.

[`String.prototype.toLowerCase()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase)Returns the calling string value converted to lower case.

[`String.prototype.toUpperCase()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase)Returns the calling string value converted to uppercase.

str.split("dog").join("cat");

### &#x20;[`String.prototype.toString()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toString)Returns a string representing the specified object. Overrides the [`Object.prototype.toString()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString) method.

### Methods[Section](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#Methods) <a href="#methods" id="methods"></a>

[`String.fromCharCode()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode)Returns a string created by using the specified sequence of Unicode values.[`String.fromCodePoint()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint)Returns a string created by using the specified sequence of code points.[`String.raw()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw) Returns a string created from a raw template string.

## typeof

**typeof null = objec**

| **Type**                         | result                     |
| -------------------------------- | -------------------------- |
| Null                             | `"object"`（见下文）            |
| Boolean                          | `"boolean"`                |
| Number                           | `"number"`                 |
| String                           | `"string"`                 |
| Symbol （ECMAScript 6 新增）         | `"symbol"`                 |
| 宿主对象（由JS环境提供）                    | *Implementation-dependent* |
| 函数对象（\[\[Call]] 在ECMA-262条款中实现了） | `"function"`               |
| 任何其他对象                           | `"object"`                 |

New

```
var str = new String('String');
var num = new Number(100);
typeof str; // It will return 'object'
typeof num; // It will return 'object'
```

Float

parseFloat(String s) return float

## JSON

JSON.stringify() Convert a JavaScript object into a string

JSON.parse()  Convert text into a JavaScript object:
