DEV Community

Cover image for ARRAY IN PROGRAMMING
Vinoth Kumar
Vinoth Kumar

Posted on • Edited on

ARRAY IN PROGRAMMING

ARRAY IN JS: An array is an ordered list of values. Each value, known as an element, is assigned a numeric position in the array called its index.

  • The indexing starts at 0, so the first element is at position 0, the second at position 1, and so on.
  • Arrays can hold any type of data-such as numbers, strings, objects, or even other arrays-making them a flexible and essential part of JavaScript programming.

CREATE ARRAY USING LITERAL: An array using array literal involves using square brackets [] to define and initialize the array.

EXAMPLE:

const marks = [70,75,90,99,33,];
console.log(marks);
Enter fullscreen mode Exit fullscreen mode

CREATE USING NEW KEYWORD(CONSTRUCTOR): The "Array Constructor" refers to a method of creating arrays by invoking the Array constructor function.

EXAMPLE:

const subjects = new array[70,90,99,30,75];
console.log(subjects);
Enter fullscreen mode Exit fullscreen mode

OPERATIONS ON JS ARRAY:

  1. Accessing Elements of an Array.
  2. Accessing the First Element of an Array.
  3. Accessing the Last Element of an Array.
  4. Modifying the Array Elements.

ACCESSING ELEMENTS OF AN ARRAY: Any element in the array can be accessed using the index number. The index in the arrays starts with [0].

EXAMPLE:

const marks = [50,60,70,80,90];
console.log(marks[1]);
Enter fullscreen mode Exit fullscreen mode

ACCESSING THE FIRST ELEMENT OF AN ARRAY: The array indexing starts from 0, so we can access first element of array using the index number.

EXAMPLE:

const marks = [50,60,70,80,90];
for(let i = 0; i < 5; i++){
    console.log(marks[i]);
}```



**ACCESSING THE LAST ELEMENT OF AN ARRAY:** Access the last array element using [array.length - 1] index number.

**EXAMPLE:**



Enter fullscreen mode Exit fullscreen mode

let frontend = ["HTML", "CSS", "JS"];
let access = frontend[frontend.length - 1];
console.log("Last Item: ", access);


plaintext

**MODIFYING THE ARRAY ELEMENTS:** An array can be modified by assigning a new value to their corresponding index.

**EXAMPLE:**



Enter fullscreen mode Exit fullscreen mode

const marks = [50,60,70,80,90];
marks[3] = 75;
console.log(marks);




Enter fullscreen mode Exit fullscreen mode

Top comments (0)