OneBite.Dev - Coding blog in a bite size

declare an array in Javascript

Code snippet on how to declare an array in Javascript

var arrayName = [];

This code creates a new empty array called arrayName. This type of initialization is most commonly used to declare an empty array, but you can also add elements to the array while declaring it. The array is declared using the keyword “var” followed by the array name and then the array elements in [ ]. Note that the array elements must be separated with a comma. When the array is declared, it can be used like any other array. You can add, remove, loop through, and do any other operations on the array. To add items to the array, you can use the arrayName.push(element) method. To remove items from the array, use arrayName.splice(index, 1). The index parameter is the index of the array element you want to remove and the second parameter is the number of elements to remove. You can loop through the array using a for loop like this: for (var i = 0; i < arrayName.length; i++) { // your code here }.

javascript