Node.js Series Overview
- Increase the Memory Limit for Your Process
- Why You Should Add “node” in Your Travis Config
- Create a PDF from HTML with Puppeteer and Handlebars
- Create Your Own Custom Error
- Extend Multiple Classes (Multi Inheritance)
- Get a File’s Created Date
- Get a File’s Last Modified/Updated Date
- Write a JSON Object to a File
- How to Create an Empty File
- How to Merge Objects
- Retrieve a Request’s IP Address in Node.js
- Callback and Promise Support in your Node.js Modules
- Run Async Functions/Promises in Sequence
- Run Async Functions/Promises in Parallel
- Run Async Functions in Batches
Assign an Empty Array
A common way to reset an array is assigning an empty array:
let items = [ 'item1', 'item2', 'item3' ] items =  console.log(items.length) // -> output: 0
This comes with the downside of creating a new array and leaving the old one for the garbage collector. Garbage collection is an optimized process in nowadays Node.js runtimes. It won’t create much headache for you as a developer.
Using a new array will not affect any value that references your array. Depending on where this array comes from this is safe to use because you’re not changing the values themselves.
Set the Array Length to Zero
Setting the length to zero will empty the existing array. It will also empty all participants.
let items = [ 'item1', 'item2', 'item3' ] items.length = 0 console.log(items.length) // -> output: 0