Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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?
Here are some ways to empty an array:
myArray = []; myArray = new Array(); myArray.length = 0; myArray.splice(0, myArray.length);
These will all work.
Add a comment
myArray.length = 0; will empty the array. You can then repopulate it as needed.
myArray.length = 0;
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 = [];
myArray = [];
Required, but never shown
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.
Explore related questions
See similar questions with these tags.