13
Defining & Creating Arrays in JavaScript
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
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
.
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
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.
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.
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"
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:
let arr = [item1, item2, item3,....];
The following example shows an one-dimensional array which contains booleans, strings, and numbers:
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.
let complexArr = [
'Swarnali', 33, true, ['Roy', 93], {one: 1, two: "2"}
];
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 :
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:
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:
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.
13