JavaScript Empty Array: How to Clear an Array in JavaScript

While programming, the programmers ofter have to deal with many variables. However, it is sometimes inconvenient to make so much number of variables. So instead, the programmers ofter prefer to use the array.

The array helps to accept and store a sequence of inputs. Thus, the programmer does not have to make many variables with similar properties. For example, suppose there are 100 students in the class, and we want to accept the marks obtained by each in mathematics. But it will take 100 variable names to store the marks.

As a result, it is much more convenient to store the marks in the array instead of where they will be written as a sequence of elements.

Like  marks = [54, 65, 57, 46……]     (100 elements in the array). The arrays have another special property in that they have fixed lengths. So we need to use special methods to clear the array in javascript.

JavaScript clear array

To clear or empty an array in JavaScript, assign an empty array to a non-empty array variable. It will update the non-empty array to an empty array and hence clear the array indirectly.

Example

let x = [23, 45, 66];

let y = [];

x = y;

console.log(x);

Output

[]

Explanation

We created an array named x using the let keyword. Then we created another empty array named y using the let keyword. Now we assigned the value of empty variable y to the array x. So the array x now becomes empty.

Setting the length of the array to zero.

To empty an array in JavaScript, set the length of the array to zero. If we update the length of the array to zero, the array will now hold no elements, and hence the array is cleared.

Example

let x = [23, 45, 66];

x.length = 0;

console.log(x);

Output

[]

Explanation

In the above code, we initialized an array named x using the let keyword. Now we use the x.length=0 method, which updated the length of the array to 0. As a result, the array is cleared.

Using splice() method

JavaScript array splice() is a built-in method that removes the array elements and clears the array. The splice() method contains two important parameters. The first parameter determines how many elements will be kept in the array. The second parameter determines the length to be traversed in the array.

Example

let x=[23,45,66];

x.splice(0,arr.length);

console.log(x);

Output 

[]

Explanation

In the above code, we have first created an array named x using the let keyword. Now we have used the x.splice() method. First, we have given the argument 0 to specify that we want no element in the array to be left. The second parameter we have passed is the total length of the array to be traversed throughout the process.

We could have passed other arguments like (1, arr. length). However, in this case, the first element should have been left in the array.

Example

let x=[23,45,66];

x.splice(1,x.length);

console.log(x);

Output

[23]

Conclusion

In this article, we learned how to clear the array in JavaScript. Sometimes the programmers need to clear the array to dynamically use the same array name in the program, especially in the loop.

Related posts

How to Get Index of Element in Array in JavaScript

JavaScript filter array with multiple conditions

How to Prevent Duplicates in Array in JavaScript

Leave a Comment