Defining & Creating Arrays in JavaScript
Tags: javascript, beginners, webdev, codenewbie
Published: 2021-07-22 18:06:19

Hi dear readers! 
This blog is mainly about **Basic Data Structures in JavaScript**. *Data Structure* is a particular way of organizing data in computer from where we can access them and use them effectively.

In this series , we will be familiar with a very commonly used data structure : **Arrays**

###Definition of an Array

An *Array* is a collection of items stored at contagious memory locations. It can hold more than one value at a time. Each value is called an *element* specified by an *index*. 
Arrays can be `one-dimensional` or `multi-dimensional`.

####One-dimensional Array

A `one-dimensional array` is a type of *linear array*. Accessing its elements involves a single subscript which can either represent a row or column index. In other words, it only has one level which means it does not have any other arrays nested within it

####Multi-dimensional Array

A multi-dimensional array is an *array of arrays*. In other words, it has multiple levels which means it includes other arrays nested within it. `Two-dimensional arrays` are most commonly used, also known as a *table* or *matrix*. A two-dimensional array associates each of its elements with two indexes.

###Characteristics of an Array in JavaScript

In an JS Array, we can observe two important characteristics.

1) *First of all* , an array can hold values of different `data types`. For example, we can have an array that can store *numbers*, *strings*, *boolean values*, *arrays* or even *Objects*.

2) *Secondly*, the length of an array is dynamically sized and it grows automatically with increasing number of data. Basically, we can say that we need not to specify the *array size* upfront.

### Creating An Array

Arrays can be created in two different ways in JavaScript. One is using `array literal notation` & another is using `Array Constructor with the Keyword "new"`

#### Array Literal Notation

The most popular and easy way of creating an JS array is using an *Array Literal Notation*. The *array literal* form uses the *square brackets [ ]* to wrap a comma-separated list of elements/items.
The *basic syntax* is: 
```js
let arr = [item1, item2, item3,....];
```

The following example shows an *one-dimensional array* which contains *booleans*, *strings*, and *numbers*: 

```js
let simpleArr = ['Swarnali', 93, true,'Roy', false, 8];
```
Let's see another example which shows a complex *multi-dimensional array*. *To be noted*, the last element of this array is an `JavaScript Object`, which we will learn later in another blog. But for now, we need to know that arrays are capable of storing complex objects too.

```js
let complexArr = [
'Swarnali', 33, true, ['Roy', 93], {one: 1, two: "2"} 
];
```

#### Array Constructor with the Keyword "new"

There are different syntax for creating an array with *Array Constructor*.
If we want to declare an empty array, that is an array with no elements the *syntax* will be : 
```js
let rainbow = new Array();
```
If we know how many elements will the array hold, that means, the size of an array, we can create an array with an initial *size* as shown in the code snippet below:
```js
let rainbow = new Array(7);
```
To create an array with some elements, we need to pass the elements as a comma-separated list into the `Array() constructor`.
The following *example* creates the *rainbow* array that has *seven* elements:

```js
let rainbow = new Array('Violet','Indigo','Blue','Green','Yellow','Orange','Red');
```

**Notice that,**
If we use the array constructor to create an array and pass into *only one number*, we are basically creating an array with an initial size. 

On the other hand, while passing several numbers , for example `let numbers = new Array(1,2,3)`,it creates an array with *three numbers* as it's elements.  

However, passing a value of another type like *string* into the Array constructor, we can create an array with an element of that value.

##### For simplicity, readability and execution speed, we use the first one (the array literal method). The second method is used rarely.