Navigate back to the homepage

how to empty an array in javascript?

Infinitbility
JavaScript
August 2nd, 2022 · 1 min read
how to empty an array in javascript?

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To empty an array in javascript, use the splice() method with array length it will delete all elements of the array without compromise with reference, and also it is one of the fastest ways to empty an array.

The splice() method takes the index (from which the splicing should start ) and the number of items to be removed as parameters and splices the elements.

We have to pass the 0 as an index (the first element) and the length of the array as parameters that ends up emptying the whole array. The performance of this method is almost as fast as assigning the new array method

1array.splice(0, array.length)

Today, I’m going to show you How do I empty an array in javascript, as above mentioned, I’m going to use the above-mentioned splice() method to empty the array.

Let’s start today’s tutorial how do you empty an array in javascript?

Chrome - How to Clear Cache

Javascript empty an array example using splice() method

Here, we will create an array with some data, and then we will use the splice() method to remove all elements of the array.

1let arr = ["Infinitbility","aguidehub","sortoutcode"]
2arr.splice(0, arr.length)
3console.log(arr)
4// output: "[]"

I hope it helps you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

how to empty an array in react js?

how to empty an array in react js?

An example of an empty array in react js with the step-by-step guide.

August 2nd, 2022 · 1 min read
how to empty an array in react native?

how to empty an array in react native?

An example of an empty array in react native with the step-by-step guide.

August 2nd, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]