|
| 1 | +## 11.27 Arrays |
| 2 | +Consider a scenario where you want to store the names of 100 employees within an IT department. This can be done by creating 100 variables and storing the names. However, keeping track of 100 variables is a tedious task and it results in inefficient memory utilization. The solution to this problem is to create an array variable to store the names of 100 employees. |
| 3 | +![[Figure 11.33-Memory Utilization Using an Array.png]] |
| 4 | +An array is a collection of values stored in adjacent memory locations. These array values are referenced using a common array name. The values of an array variable must be of the same data type. These values that are also referred to as elements and can be accessed by using subscript or index numbers. The subscript number determines the position of an element in an array list. |
| 5 | + |
| 6 | +JavaScript supports two types of arrays that are as follows: |
| 7 | +- Single-dimensional array |
| 8 | +- Multi-dimensional array |
| 9 | +#### What is Adjacent Memory Location (AML) |
| 10 | +Think of computer memory like a **row of lockers**: |
| 11 | +- Each locker stores a small piece of data. |
| 12 | +- Lockers are numbered in order: 1, 2, 3, 4, … |
| 13 | +If a program (process) uses lockers **3, 4, 5, and 6**, those lockers are **adjacent** because they are **side by side**. |
| 14 | +![[image212132s.png]] |
| 15 | +## 11.27.1 Single-Dimensional Array |
| 16 | +In a single-dimensional array, the elements are stored in a single row in the allocated memory. |
| 17 | +![[Figure 11.35-Single-Dimensional Array.png]] |
| 18 | +As shown in figure 11.35, the first element has the index number zero and the last |
| 19 | +array element has an index number one less than the total number of element. This arrangement helps in efficient storage of data, In addition, it also helps to sort data easily and track the data length. The array variable can be created using the `Array` object and `new` keyword along with the size of the array element. |
| 20 | + |
| 21 | +The syntax to declare and initialize a single-dimensional array is as follows: |
| 22 | +**Syntax** |
| 23 | +``` |
| 24 | +var variable_name = new Array(size); //Declaration |
| 25 | +variable_name[index] = 'value'; |
| 26 | +``` |
| 27 | +**where**, |
| 28 | +`variable_name`: Is the name of the variable. |
| 29 | +`size`: Is the number of elements to be declared in the array. |
| 30 | +`index`: Is the index position. |
| 31 | + |
| 32 | +**Code Snippet 36:** |
| 33 | +Code Snippet 36 shows different ways to declare and initialize a single-dimensional array. |
| 34 | +``` |
| 35 | +<script> |
| 36 | + // Declaration using Array Pbject and then Initialization |
| 37 | + varmartial_status = newArray(3); |
| 38 | + martial_status[0] = 'Single'; |
| 39 | + martial_status[1] = 'Married'; |
| 40 | + martial_status[2] = 'Divorced'; |
| 41 | + |
| 42 | + // Declaration and Initialization |
| 43 | + varmartial_status = newArray('Single', 'Married', 'Divorced'); |
| 44 | + |
| 45 | + // Declaration and Initialization Without Array |
| 46 | + varmartial_status['Single', 'Married', 'Divorced']; |
| 47 | +</script> |
| 48 | +``` |
| 49 | +## 11.28 Array Methods |
| 50 | +An array is a set of values grouped together and identified by a single name. In JavaScript, the `Array` object allows you to create arrays. It provides the `length` property that allows you to determine the number of elements in the array. Various methods of the `Array` object allow to access and manipulate the array elements. |
| 51 | + |
| 52 | +| Method | Description | |
| 53 | +| -------- | ---------------------------------------------------- | |
| 54 | +| `concat` | Combines one or more array variables. | |
| 55 | +| `join` | Joins all the array elements of an array. | |
| 56 | +| `pop` | Retrieves the last element of an array. | |
| 57 | +| `push` | Appends one or more elements to the end of an array. | |
| 58 | +| `sort` | Sorts the array elements in an alphabetical order. | |
| 59 | + |
0 commit comments