➤ How to Code a Game
➤ Array Programs in Java
➤ Java Inline Thread Creation
➤ Java Custom Exception
➤ Hibernate vs JDBC
➤ Object Relational Mapping
➤ Check Oracle DB Size
➤ Check Oracle DB Version
➤ Generation of Computers
➤ XML Pros & Cons
➤ Git Analytics & Its Uses
➤ Top Skills for Cloud Professional
➤ How to Hire Best Candidates
➤ Scrum Master Roles & Work
➤ CyberSecurity in Python
➤ Protect from Cyber-Attack
➤ Solve App Development Challenges
➤ Top Chrome Extensions for Twitch Users
➤ Mistakes That Can Ruin Your Test Metric Program
1. Replace with a new empty array inplace of the existing array.
2. Set the length parameter for an array to 0.
3. Splice the entire array.
After Replacing with empty array:
The lang = [ ] assigns a variable a reference to a fresh array. Other references, however, are unaffected, which results in the retention of references to the data in a prior array, which causes memory leaks.
Modify the array size to 0 to clear the array. In this manner, the array will always be cleared and will not contain a single item. Every element in the array is removed, which does affect other entries.
Basically, if you use an array to remove the elements of an array and have two pointers to the same array (a = [‘cat’, ‘dog’, ‘parrot’]; a2 = a;). The pointers (a and a2) will now both refer to the same empty array if array.length = 0.
let pet = ['cat', 'dog', 'parrot']; let set = ['cat', 'dog', 'parrot']; let pet2 = pet; let set2 = set; pet = ; set.length = 0; console.log(pet, set, pet2, set2);
[ ] [ ] [ ‘cat’, ‘dog’, ‘parrot’ ] [ ]
let lang = ['C++', 'Java', 'PHP']; lang.splice(0, lang.length) console.log(lang)
We have used the splice() method by supplying the array size as the second argument and 0 as the first argument. This will clear the original array and provide a replica of the original elements, which may be helpful in some situations. However, it will also erase every item from the array.
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!