4 Ways to Empty an Array in Node.js

4 Ways to Empty an Array in Node.js

There are various ways to empty an array of all it's contents. We'll show you each of the four ways to empty an Array in Node.js in this article.

Originally published by Nick Major at https://coderrocketfuel.com

Introduction

There are various ways to empty an array of all it's contents. We'll show you each of the four ways in this article.

Let's get started!

Table of Contents

  1.  Create a New Empty Array
  2.  Set Length To Zero
  3.  Using Splice()
  4.  Loop Over & Remove Each Value

1. Create a New Empty Array

This method will create a brand new array and is the fastest method of the four because it doesn't change anything in the original array.

Here's the code:

let names = ["Johnny", "Billy", "Sandy"]
 
names = []
// []

You should be careful with this method because if you've referenced the original array from another variable or property, the original array will remain unchanged. Only use this if you only reference the array by its original value.

2. Set Length To Zero

This method uses the .length method to set the length of the array to zero.

This is the second-fastest method of the four.

Here's the code:

const names = ["Johnny", "Billy", "Sandy"]
 
names.length = 0
// []

3. Using Splice()

This method uses the splice() method to return the array with all the removed items.

Here's the code:

const names = ["Johnny", "Billy", "Sandy"]
 
names.splice(0, names.length)
// []

This method will return a copy of the original array. But it doesn't have much of an effect on performance.

4. Loop Over & Remove Each Value

This method will use a while loop to pop() each item in the array. This is both the slowest and least succinct method of the four.

Here's the code:

const names = ["Johnny", "Billy", "Sandy"]
 
while (names.length > 0) {
  names.pop()
}
// []

Conclusion

There you have it! These are four ways to empty an array in Node.js.

Did we miss any methods you know of? Let us know in the comments!

As always, thanks for reading!

If you liked this post, share it with all of your programming buddies!

Follow us on Facebook | Twitter

Further reading

The Complete Node.js Developer Course (3rd Edition)

Angular & NodeJS - The MEAN Stack Guide

NodeJS - The Complete Guide (incl. MVC, REST APIs, GraphQL)

Best 50 Nodejs interview questions from Beginners to Advanced in 2019

Node.js 12: The future of server-side JavaScript

An Introduction to Node.js Design Patterns

Basic Server Side Rendering with Vue.js and Express

Fullstack Vue App with MongoDB, Express.js and Node.js

How to create a full stack React/Express/MongoDB app using Docker

node-js web-development javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Node.JS Developers | Skenix Infotech

We are providing robust Node.JS Development Services with expert Node.js Developers. Get affordable Node.JS Web Development services from Skenix Infotech.

How to Hire Node.js Developers And How Much Does It Cost?

A Guide to Hire Node.js Developers who can help you create fast and efficient web applications. Also, know how much does it cost to hire Node.js Developers.

Top Node.js Development Companies and Expert NodeJS Developers

A thoroughly researched list of top NodeJS development companies with ratings & reviews to help hire the best Node.JS developers who provide development services and solutions across the world. List of Leading Node.js development Service Providers...

Hire Web Developer

Looking for an attractive & user-friendly web developer? HourlyDeveloper.io, a leading web, and mobile app development company, offers web developers for hire through flexible engagement models. You can **[Hire Web...

Top Vue.js Developers in USA

Vue.js is an extensively popular JavaScript framework with which you can create powerful as well as interactive interfaces. Vue.js is the best framework when it comes to building a single web and mobile apps.