Js Array, how to use array, Types of Array and Storing values in array

Js array

Array:

Js array is a consecutive group of memory locations with the same name. the memory locations in an array are referred to as elements of the array. The total number of elements in the array is called the size of the array (or its length).

Each location or element in the array is accessed by specifying the name of the array and the position number of the particular element in the array. This position number is called index or subscript. In JavaScript, index is written within square brackets ([]) with the name of the array.

In JavaScript(Js), the first element of the Js array has an index value of 0. The index of the first element of the array is called its lower bound (LB) and the index of the last element of the JavaScript array is called it’s Upper Bound (UB).

Js array is used to process a large amount of data of the same type. The data is stored in an array. The JavaScript array is accessed by a single variable name. the index values are used to access individual elements of the array. Typically, a loop statement is used to process data in an array by changing index value in the body of the loop to access all elements of the array. A few statements are required to process data in an array. Therefore, to process data using arrays in a program reduces the size of the program.



Array in JavaScript:

In JavaScript, an array is considered as an object. It allows us to store multiple values in a single variable. By definition, a Js array is a collection of the same type data. However, the JavaScript array is considered a sequence of values. The data type of these values may be of different types.

Type of Js array:

Js arrays are divided into two types:

  1. One-dimensional arrays
  2. Multi-dimensional arrays

One-Dimensional JavaScript Js array:

One-dimensional Js array is also known as a linear array. It consists of only one row or one column. For example, the temperature of each day of a month is stored in an array. The name of the array is “day” and its elements are day[0], day[1], day[2],—-,day[30].

Js array

The above array name “day” contains floating-point data ( or real data). It has 31 elements. The day[0] is the first element of the Js array  and its value is 5.2. similarly, day[30] is the last element of the JavaScript array and its value is 8.1.

We know that a Js array occupies a contiguous block of memory and it is divided into equal parts. Each part of the memory block represents one element of the JavaScript array. In JavaScript, elements are numbered from 0 to n-1. The ‘n’ represents the total number of elements of the Js array.

Declaring One-Dimensional Js array :

Like other variables in javascript, an array must be declared before it is used. Defining the name of Js array and its total number of elements is called declaring of the array. When a JavaScript array is declared, a memory block with the required number of consecutive locations is reserved in the computer memory for storing data.

In JavaScript, the new operator is used to create an array. Actually, this operator is used to create a new object. JavaScript considers the array to be an object data type.


Techniques for declaring Js array :

There are two techniques are used to declared the Js array .

First method:

In this way, the size of the Js array is specified at the time of its declaration. The general syntax to declare a JavaScript array using this method is:

ArrayName= new Array(size);

Where:
ArrayName:

Specifies the name of array.

Array:

 It is the keyword of JavaScript.

Size:

Specifies the size of array i.e. number of elements in the array.

For example to declare an array “xyz” of size 10, the statement is written as;

Xyz= new array(10);

Second Method:

 In this way, the Js array is declared without specifying its size. The size of the array is zero. The general syntax to declare a Js array  using this method is:

ArrayName = new Array();

In this technique, JavaScript automatically extends the size to an array when new Js array elements are initialized. For example, the following statements create an array “arr” of size 0 and then extend the sizes of this array  to 50 and then 500:

arr= new Array();

arr[49]= 345;

arr[499]= 925;

please note that if the size of the array  is specified at the time of its declaration, its size can also be extended. This is done in the same way as with a zero-size array. For example, the following statement creates an Js array  “abc” of size 10 and then extend the size of this array to 30:

abc= new Array(10);

abc[29] = 139;

in the above statement “abc[29] = 139”, the size of array “abc” is extended by referencing its element 29 which is outside the actual size specified.

Using One-Dimensional Js array:

The Js array is used to process large amounts of data. Each element of an array is accessed by specifying the name of the JavaScript array and its index value. you can store data in an array using input statements with the help of a loop structure. The index value of array is changed through the loop statement. You can input data into elements of JavaScript array or display data elements of the array. Similarly, you can search a particular value from the array. The values of JavaScript array can also be sorted. Before, to perform different operations on the array, first of all, values are stored in an array.


Storing values in an one-dimensional Js array :

In JavaScript, once you have declared an array, it can be filled with values of any data types. The values are stored in a Js array using the assignment statement or input statement. In JavaScript, you can use a “prompt” dialog box to input data into the elements of the JavaScript array during the execution of the script.

You can also assign value to the individual elements of an array using an assignment statement. For example, to assign values to an array “temp” having 4 elements, the following assignment statements are used:

Temp[0] = 15;

Temp[1] = 20;

Temp[2] = 28;

Temp[3] = 30;

 the array may have several elements. Usually, the loop statement is used to access all elements of JavaScript array. A single assignment statement or input statement is used within the body of the loop to assign data to all elements by changing the index value.

For example, to fill all the elements of array “abc” with value 50, the program is written as:

Example write JavaScript code to input values into individual elements of an array during the execution of the script and display the values of Js array:

Js array

Js array

Js array

Js array

Js array

Js array



Displaying Data From Js array :

The output statements are used to display the contents to a Js array. In JavaScript, you can display the contents of JavaScript array using “document.write()” command, alert and confirm dialog boxes.

For example, to assign values to array “abc” and to display the contents of array in the browser’s window, the code is given below:

In the above code, the assignment statements are used to assign values to the elements of the JavaScript array. Similarly, the output statement is used inside the for loop structure to print the value of all elements of Js array “temp”.

Example write JavaScript code to enter integer type data into an array and then to print the values in reverse order:

Js array

Js array

Js array

Js array

Js array

Js array


Example write JavaScript code to enter values into one-dimensional array. Find out the total number of odd and even values enter into one-dimensional array:

Js array

Js array

Enter values 10 times in prompt

Js array

Js array

Dense Js Array:

In JavaScript, an array that is declared and initialized with values at the time of its declaration is called dense Js array. In other programming languages, assigning values to the elements of the JavaScript array at the time of its declaration is called array initialization. A dense JavaScript array is used exactly in the same manner as other arrays.

The general syntax to create a dense Js array  is;

Arrayname = new Array(list of values);

In the above syntax, “list of values” represents the list of constant values separated by commas.

For example, an array containing three values is created as follows:-

abc = new Array(‘english’ , ‘math’ , ‘computer’);

the size of the above Js array  will be 3. You can modify the size of above array.


Example write JavaScript code to initialize values 2, 6, 7, 8, 4 and 3 in one dimensional array which compute the factorial of the values of each element of Js array and print the result:

Js array

Related Article:

JavaScript while loop and do-while loop with programming examples

Recommended For You

Fawad

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

Leave a Reply

%d bloggers like this: