0

I am using:

var myArry = [];

as a global variable. I then populate the array with some data from the database. If I then want to replace the array data with new data do I have to empty / reset the original array.

Or what is the correct method?

3 Answers 3

4

Here are some ways to empty an array:

myArray = [];
myArray = new Array();
myArray.length = 0;
myArray.splice(0, myArray.length);

These will all work.

Sign up to request clarification or add additional context in comments.

Comments

2

myArray.length = 0; will empty the array. You can then repopulate it as needed.

Comments

1

If it comes from the database, it would be better to clear your array since some data may be deleted.

To do so, you can write myArray = [];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.