array = [] vs. array.length = 0

Introduction 

 
In this short blog post, we'll understand the difference between the two ways of emptying an object in JavaScript i.e. by assigning an empty array and setting the length property of an array to zero.
  1. // set length property to zero.  
  2. myArray.length = 0;  
  3.   
  4. // assign empty array  
  5. myArray = [];   
To know more about the length property, visit here.
 
Assigning an empty array points the existing variable to a new reference instead of modifying the existing reference.
  1. let myArray = [1, 2, 3, 4, 5];  
  2. let myArray2 = myArray;  
  3.   
  4. myArray = [];  
  5.   
  6. console.log(myArray2);  
  7. // Output: [1, 2, 3, 4, 5]  
However, if you set the length property to zero it modifies the existing array and all the variables pointing towards it. 
  1. let myArray = [1, 2, 3, 4, 5];    
  2. let myArray2 = myArray;    
  3.   
  4. myArray.length = 0;    
  5.   
  6. console.log(myArray2);  
  7. // Output: []