# Js Array Common Method 1

### 1. Join

The `join()` method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.

{% tabs %}
{% tab title="JavaScript" %}
```javascript
const elements = ['Fire', 'Air', 'Water'];

console.log(elements.join());
// expected output: "Fire,Air,Water"

console.log(elements.join(''));
// expected output: "FireAirWater"

console.log(elements.join('-'));
// expected output: "Fire-Air-Water"
```
{% endtab %}
{% endtabs %}

### 2. reverse

The `reverse()` method reverses an array in place. The first array element becomes the last, and the last array element becomes the first.

{% hint style="danger" %}
Reverse is destrutive
{% endhint %}

{% tabs %}
{% tab title="JavaScript" %}
```javascript
const array1 = ['one', 'two', 'three'];
console.log('array1:', array1);
// expected output: "array1:" Array ["one", "two", "three"]

const reversed = array1.reverse();
console.log('reversed:', reversed);
// expected output: "reversed:" Array ["three", "two", "one"]

// Careful: reverse is destructive -- it changes the original array.
console.log('array1:', array1);
// expected output: "array1:" Array ["three", "two", "one"]
```
{% endtab %}
{% endtabs %}

### 3. sort

The `sort()` method sorts the elements of an array in place and returns the sorted array. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.

The time and space complexity of the sort cannot be guaranteed as it depends on the implementation.

{% tabs %}
{% tab title="JavaScript" %}
```javascript
const months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort();
console.log(months);
// expected output: Array ["Dec", "Feb", "Jan", "March"]

const array1 = [1, 30, 4, 21, 100000];
array1.sort();
console.log(array1);
// expected output: Array [1, 100000, 21, 30, 4]
```
{% endtab %}
{% endtabs %}

#### Parameters

**compareFunction Optional**&#x20;

Specifies a function that defines the sort order. If omitted, the array elements are converted to strings, then sorted according to each character's Unicode code point value.

* If `compareFunction(a, b)` returns a value > than 0, sort `b` before `a`.
* If `compareFunction(a, b)` returns a value < than 0, sort `a` before `b`.
* If `compareFunction(a, b)` returns 0, `a` and `b` are considered equal.

{% tabs %}
{% tab title="JavaScript" %}
```javascript
var numbers = [4, 2, 5, 1, 3];
numbers.sort(function(a, b) {
  return a - b;
});
console.log(numbers);

// [1, 2, 3, 4, 5]
```
{% endtab %}
{% endtabs %}

Use arrow function expressions with shorter syntax

{% tabs %}
{% tab title="JavaScript" %}
```javascript
let numbers = [4, 2, 5, 1, 3];
numbers.sort((a, b) => a - b);
console.log(numbers);

// [1, 2, 3, 4, 5]
```
{% endtab %}
{% endtabs %}

### 4. Push / Pop and Shift / Unshift

The **`push()`** method adds one or more elements to the end of an array and returns the new length of the array.

The **`pop()`** method removes the **last** element from an array and returns that element. This method changes the length of the array.

The **`shift()`** method removes the **first** element from an array and returns that removed element. This method changes the length of the array.\
\
The **`unshift()`** method adds one or more elements to the beginning of an array and returns the new length of the array.

![twitter @profulsadangi](https://3069112348-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR93ctAFfSCsQwng6wL%2F-Mjdl99gk9G6H4-_GT9_%2F-MjdmJuEsgHbbmQZcuaT%2Fimage.png?alt=media\&token=8d544456-083b-4497-9188-0e4478290d6e)

### 5. find

The `find()` method returns the value of the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, `undefined` is returned.

{% tabs %}
{% tab title="JavaScript" %}
```javascript
const array1 = [5, 12, 8, 130, 44];

const found = array1.find(element => element > 10);

console.log(found);
// expected output: 12
```
{% endtab %}
{% endtabs %}
