# Remove an element from array in JavaScript

The following snippets allows you to remove an element from array in JavaScript.

## Method 1: Using `pop()`*

```let myArray = [1, 2, 3, 4, 5]
myArray.pop()

console.log(myArray) // [1, 2, 3, 4]```

Removes an element from the end of the array and mutates itself.

## Method 2: Using `shift()`*

```let myArray = [1, 2, 3, 4, 5]
myArray.shift()

console.log(myArray) // [2, 3, 4, 5]```

Removes an element from the start of the array and mutates itself.

## Method 3: Using `filter()`

```let myArray = [1, 2, 3, 4, 5]
myArray = myArray.filter((element) => element !== 3)

console.log(myArray) // [1, 2, 4, 5]```

Removes an element based on the condition and returns a new array. In this example, we only returned array elements not equal to 3.

## Method 4: Using `length`

```let myArray = [1, 2, 3, 4, 5]
myArray.length = 2

console.log(myArray) // [1, 2]```

Removes an element based on the indices of the elements. In this example, we removed elements starting with index 2 and so on.

Setting the array length to 0 means the array will become empty.

## Method 5: Using `splice()`*

```let myArray = [1, 2, 3, 4, 5]
myArray.splice(0, 2)

console.log(myArray) // [3, 4, 5]```

Removes an element based on the indices of the elements. In this example, we removed 2 elements starting from index 0.

```let myArray = [1, 2, 3, 4, 5]
myArray.splice(2, 2)

console.log(myArray) // [1, 2, 5]```

Removes an element based on the indices of the elements. In this example, we removed 2 elements starting from index 2.

## Method 6: Using delete operator

```let myArray = [1, 2, 3, 4, 5]
delete myArray

console.log(myArray) // [1, 2, undefined, 4, 5]```

Removes only the value of an element based on the index of the element and returns the same length of array as the original. In this example, we removed the value of index 2.

Do you have any more ways to remove an element from array in JavaScript? Share it on the comments below.
{"ft_lang_label":"__label__en","ft_lang_prob":0.698002,"math_prob":0.9766976,"size":1919,"snap":"2021-43-2021-49","text_gpt3_token_len":594,"char_repetition_ratio":0.19791123,"word_repetition_ratio":0.28947368,"special_character_ratio":0.318395,"punctuation_ratio":0.2072893,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96436006,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-11-27T23:37:53Z\",\"WARC-Record-ID\":\"<urn:uuid:9ad147bc-a14a-4e11-a5d7-5a5afd7324ef>\",\"Content-Length\":\"41807\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:d1c3c32a-d40f-4442-b644-19e252c39c31>\",\"WARC-Concurrent-To\":\"<urn:uuid:a332a582-9f1e-4c5a-9245-c3347c783bc6>\",\"WARC-IP-Address\":\"104.21.38.199\",\"WARC-Target-URI\":\"https://www.davidangulo.xyz/snippets/remove-an-element-from-array-in-javascript/\",\"WARC-Payload-Digest\":\"sha1:MRHCUPQFRBCZ5PUA7JYSB4N3KQIWKQTF\",\"WARC-Block-Digest\":\"sha1:R2TUOFO4GIBWS43KS2RMLCLYNAJ44DKQ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-49/CC-MAIN-2021-49_segments_1637964358323.91_warc_CC-MAIN-20211127223710-20211128013710-00041.warc.gz\"}"}