Introduction

  • In JavaScript, an array is a multivariant & linear container to store different types of data.

Definition

  • In JavaScript, an array is a data structure that can store a collection of values of any data type, including numbers, strings, booleans, objects, and other arrays at a contiguous location with a common name.

Characteristics

  • An array is created by enclosing a comma-separated list of values within square brackets [ ].
  • Arrays are a type of object in JavaScript, but they have some special properties and methods that make them useful for working with collections of data.
  • arrays in JavaScript are dynamic, meaning they can grow or shrink in size as needed, and they can hold any type of data, including other arrays or objects.
  • Each data element in the array is referenced by its position in the array (called its index number)
  • Individual array elements can be referenced/represented by the array name followed by the pair of square brackets having its index number.
  • The index number starts with zero in JavaScript i.e. the first element in JavaScript has its index value of 0, the second has its index value of 1, and so on.
  • In other words, an array is initialized with the specified values as its elements, and its length is set to the number of arguments specified. 

Syntax

  • An array can be declared in JS in different ways: –
(i) Using the Array constructor:
Syntax : var x = new Array( value1, value2 … value n);
             const x = new Array( value1, value2 … value n);
(ii) Using an Array Literal notation:
Syntax : var x = [ value1, value2 … value n];
             const x = [ value1, value2 … value n];
(iii) Creating an Empty Array and adding elements to it later:
Syntax : const x= [];         // or var x= [];
             x.push(“India”);
             x.push(500);

Example

(a)

<script>
      // var k = new Array( 10,20,30,40);  //Also work

     // let k = new Array( “A”,”B”,”C”,”D”);  //Also work

      var k = new Array( 10,23.36,”A”,”Welcome”);
      document.write(k[0]);             // Output: 10
      document.write(k[1]);            // Output: 23.36
      document.write(k[2]);            // Output: A
      document.write(k[3]);            // Output: Welcome

</script>

(b) var k= [20, 40, 60];  

document.write(k[0]);         // Output: 20
document.write(k[1]);        // Output: 40
document.write(k[2]);       // Output: 60

—————–  OR  —————–

document.write(k[0]+” “); // Output: A
document.write(k[1]+” “); // Output: B
document.write(k[2]+” “); // Output: C

—————–  OR  —————–

var k= [‘A’, ‘B’, ‘C’];
document.write(k[0]+”</br>”); // Output: A
document.write(k[1]+”</br>”); // Output: B
document.write(k[2]+”</br>”); // Output: C

—————–  OR  —————–

<script>
     //  var k= [‘A’, ‘B’, ‘C’];  

     var k= [“A”, “B”, “C”];
    document.write(k[0]);      // Output: A
    document.write(k[1]);      // Output: B
    document.write(k[2]);      // Output: C

</script>

—————–  OR  —————–

<script>
       var k= [20, “40”, 32.36, “Welcome”];

       document.write(k[0]);       // Output: 20
       document.write(k[1]);      // Output: 40
      document.write(k[2]);     // Output: 32.36
      document.write(k[3]);    // Output: Welcome

</script>

Types of Array in JS

  • In JavaScript, there are three types of arrays:-
(I)Typed Arrays:
    • Typed arrays are arrays of a single data type and are used to represent raw binary data.
    • They are particularly useful for handling large amounts of data efficiently.
    • There are several types of typed arrays in JavaScript, including Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array, and BigInt64Array.
    • For example –

const arr = new Int8Array([1, 2, 3, 4, 5]);
document. write(arr);           // Output: Int8Array [1, 2, 3, 4, 5]

(II)Array-Like Objects (Array Objects) :
    • Array-like objects are objects that have a length property and can be accessed using numeric indices.
    • They may or may not have other properties and methods of an array.
    • Examples of array-like objects in JavaScript include the arguments object and the NodeList returned by the  querySelectorAll method.
    • For example – 

function display() {
document. write(arguments);
}

display(10, 20, 30);             // Output: [10, 20, 30]

(III)Normal Arrays:
    • Normal arrays are the most commonly used type of array in JavaScript.
    • They can hold any type of data, and their size can be changed dynamically.
    • They have a number of built-in methods for manipulating the data they contain, such as push, pop, shift, unshift, slice, and splice.
    • For example – 

const arr = [100, “two”, true, {name: “USA”}];
document. write(arr); // Output: [1, “two”, true, {name: “John”}]

    Loading


    0 Comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.