Tips and Tricks for JavaScript Array

• 4 min read •

At the very first sight, I don’t like JavaScript!

And till now, I still don’t like it LOL

But because of my job, I have to deal with this it :)

In this post, I’ll show you guys some tips and tricks for JavaScript Array. Of course, I found them while wanderring on the Internet, searching for my ******* problem :)

1. Do not use new Array()

Please use [] instead of the built-in constructor new Array().

The two statements below both create an empty array:

{% highlight javascript %} var test = new Array(); // AVOID THIS WAY var test = []; // USE THIS WAY {% endhighlight %}

The two  statements below both create an array that contains 5 numbers:

{% highlight javascript %} var test = new Array(5, 9, 4, 3, 2); // AVOID THIS WAY var test = [5, 9, 4, 3, 2]; // USE THIS WAY {% endhighlight %}

The reason we abandon the built-in constructor new Array() is that It makes your code complex and can cause nasty side effects.

{% highlight javascript %} var test = new Array(11, 6); // Creates an array with two elements (11 and 6) var test = new Array(11); // Creates an array with 11 undefined elements!!!!!! {% endhighlight %}

 2. Convert JavaScript Array to CSV

JavaScript provides valueOf() method to convert an array to a CSV (Comma Seperated Value) string.

{% highlight javascript %} var cities = [‘Hanoi’, ‘Ho Chi Minh’, ‘Da Nang’, ‘Hai Phong’]; var str = cities.valueOf(); //print str: Hanoi,Ho Chi Minh,Da Nang,Hai Phong {% endhighlight %}

If you want to change the comma to any other character like *, |, -,… use join() method:

{% highlight javascript %} var cities = [‘Hanoi’, ‘Ho Chi Minh’, ‘Da Nang’, ‘Hai Phong’]; var str = cities.join(”|”); //print str: Hanoi|Ho Chi Minh|Da Nang|Hai Phong {% endhighlight %}

 3. Remove Array Element by Index

To remove an Array Element by index, we use splice() method:

{% highlight javascript %} var cities = [‘Hanoi’, ‘Ho Chi Minh’, ‘Da Nang’, ‘Hai Phong’]; //print cities: Hanoi,Ho Chi Minh,Da Nang,Hai Phong var index = 2; cities.splice(index, 1); // print cities: Hanoi,Ho Chi Minh,Hai Phong {% endhighlight %}

 4. Remove Array Element by Value

The below code snippet describe a function within Array class that allows you to remove an array element by an input value:

{% highlight javascript %} Array.prototype.removeByValue = function(val) { for(var i=0; i<this.length; i++) { if(this[i] == val) { this.splice(i, 1); break; } } }

var cities = [‘Hanoi’, ‘Ho Chi Minh’, ‘Da Nang’, ‘Hai Phong’];

cities.removeByValue(“Hanoi”); // print cities: Ho Chi Minh,Da Nang,Hai Phong {% endhighlight %}

 5. Empty a JavaScript Array

By default, JavaScript does not provide any method to empty an array.

The most common way we use to empty an arry is:

{% highlight javascript %} var cities = [‘Hanoi’, ‘Ho Chi Minh’, ‘Da Nang’, ‘Hai Phong’]; cities = []; {% endhighlight %}

This way can clear all data of cities array but it can lead to some reference problems!

For example:

{% highlight javascript %} var cities = [‘Hanoi’, ‘Ho Chi Minh’, ‘Da Nang’, ‘Hai Phong’]; var myCities = cities; cities = [];

//print cities: ” //print myCities: Hanoi,Ho Chi Minh,Da Nang,Hai Phong {% endhighlight %}

So, to empty an array correctly withou causing any side effects, just set the array length to 0! Simple, huh? :)

{% highlight javascript %} var cities = [‘Hanoi’, ‘Ho Chi Minh’, ‘Da Nang’, ‘Hai Phong’]; var myCities = cities; cities.length = 0;

//print cities: Hanoi,Ho Chi Minh,Da Nang,Hai Phong //print myCities: Hanoi,Ho Chi Minh,Da Nang,Hai Phong {% endhighlight %}

Done :D

If you have any tips and tricks for JavaScript Array, don’t hesitate to post a comment below! Thanks :)

Iced Tea Labs

A technical blog managed by a geek who loves climbing

GitHub Twitter RSS

Š 2026 Trinh Le. All rights reserved.