Empty❎ an array in JavaScript

Veröffentlicht am: 29 Juni 2024
auf dem Kanal: Mayank Srivastava
1,677
75

Hey everyone! Today, we will see how we can empty an array in JavaScript. The most obvious method is to simple reassign the array variable to an empty array, which for all intents and purposes will usually work for most of the cases but there is a minor pitfall here. if you have created a new reference variable for original array reference variable, then upon setting the original array as empty, the new reference will still remain unchanged. For example here we created a new reference to the array names called namesCopy and if we reassign names as an empty array the copy will persist the original array. To avoid this edge case a better solution is to simple set the length property of the array as zero. Not only will this clear the original array but it will also empty all the other reference variables which point to the original array. Lets use the same example but this time we will set the length of the original array as 0 and as you can see the array references were also emptied.


Auf dieser Seite können Sie das Online-Video Empty❎ an array in JavaScript mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Mayank Srivastava 29 Juni 2024 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 1,677 Mal angesehen und es wurde von 75 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!