
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to empty an array in JavaScript?
There are a couple of ways to empty an array in javascript.
Let's suppose take an array
var array1 = [1,2,3,4,5,6,7];
Method 1
var array1 = [];
The code above will set the number array to a new empty array. This is recommended when you don't have any references to the original array 'array1'. You should be careful with this way of empty the array, because if you have referenced this array from another variable, then the original reference array will remain unchanged.
Example
<html> <body> <script> var array1 = [1,2,3,4,5,6,7]; // Created array var anotherArray = array1; // Referenced array1 by another variable array1 = []; // Empty the array document.write(anotherArray); // Output [1,2,3,4,5,6,7] </script> </body> </html>
Method 2
var array1.length = 0;
The line of code above will make the length of original array to 0 there by emptying the array.
Example
<html> <body> <script> var array1 = [1,2,3,4,5,6,7]; // Created array var anotherArray = array1; // Referenced array1 by another variable array1.length = 0; // Empty the array by setting length to 0 console.log(anotherArray); // Output [] </script> </body> </html>
Method 3
array1.splice(0, array1.length);
The above line of code also works perfectly. This way of code will update all the references of the original array.
Example
<html> <body> <script> var array1 = [1,2,3,4,5,6,7]; // Created array var anotherArray = array1; // Referenced array1 by another variable array1.splice(0, array1.length); // Empty the array by setting length to 0 console.log(anotherArray); // Output [] </script> </body> </html>
- Related Articles
- In Javascript how to empty an array
- How do I empty an array in JavaScript?
- No. of ways to empty an array in JavaScript
- How to empty an array in Java
- How to assign values to an array with null/empty objects in JavaScript?
- How to create an empty array in Kotlin?
- How to create an empty array in Swift?
- How to declare an empty string array in C#?
- How to initialize an empty array list in Kotlin?
- How do you empty an array in C#?
- JavaScript to push value in empty index in array
- How to check whether an array is empty using PHP?
- How to create an empty and a full NumPy array?
- How to Check if an Array is Empty or Not in Java
- How to check if an object is empty using JavaScript?

Advertisements