Node.js Series Overview
- Callback and Promise Support in your Node.js Modules
- 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
- Human-Readable JSON.stringify() With Spaces and Line Breaks
- Write a JSON Object to a File
- How to Create an Empty File
- Run Async Functions/Promises in Sequence
- Run Async Functions/Promises in Parallel
- Run Async Functions in Batches
- How to Merge Objects
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