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:
The two  statements below both create an array that contains 5 numbers:
The reason we abandon the built-in constructor new Array() is that It makes your code complex and can cause nasty side effects.
 2. Convert JavaScript Array to CSV
JavaScript provides valueOf() method to convert an array to a CSV (Comma Seperated Value) string.
If you want to change the comma to any other character like *, | , -,… use join() method: |
 3. Remove Array Element by Index
To remove an Array Element by index, we use splice() method:
 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:
 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:
This way can clear all data of cities array but it can lead to some reference problems!
For example:
So, to empty an array correctly withou causing any side effects, just set the array length to 0! Simple, huh? :)
Done :D
If you have any tips and tricks for JavaScript Array, don’t hesitate to post a comment below! Thanks :)