Remove all cookies in JavaScript

The following snippet allows you to remove all cookies in JavaScript (client-side).

document.cookie.split(";").forEach(function(c) { document.cookie = c.replace(/^ +/, "").replace(/=.*/, "=;expires=" + new Date().toUTCString() + ";path=/"); });

I use this by pasting it directly in the browser console.

Remove an element from array in JavaScript

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

NOTE: Methods with * are mutating.

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[2]

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.

Add an element to array in JavaScript

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

NOTE: Methods with (*) are mutating.

Method 1: Using push()*

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

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

Adds an element to the end of the array and mutates itself.

Method 2: Using concat()

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

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

Adds an element to the end of the array and returns a new array.

Method 3: Using length

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

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

Adds an element to the end of the array using the next index of the last element.

Method 4: Using unshift()*

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

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

Adds an element to the start of the array and mutates itself.

Method 5: Using splice()*

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

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

Adds an element at a specific index. In this example, we used the length of the array as the index to add an element at the end of the array.

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

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

Adds an element at a specific index. In this example, we used 0 as the index to add an element at the start of the array.

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

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

Adds an element at a specific index. In this example, we used 1 as the index.

Method 6: Using the spread operator

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

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

Adds an element to the end of the array and returns a new array.

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

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

Adds an element to the start of the array and returns a new array.

Do you have any more ways to add an element to array in JavaScript? Share it on the comments below.