0% found this document useful (0 votes)
3 views2 pages

JavaScript Arrays: Basics and Methods

This document provides an overview of arrays in JavaScript, explaining their definition, usage, and syntax. It covers how to create, access, and loop through arrays, as well as common array methods for manipulation. Additionally, it introduces the concept of nested arrays for multi-dimensional data storage.

Uploaded by

Rajakumar Sai
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

JavaScript Arrays: Basics and Methods

This document provides an overview of arrays in JavaScript, explaining their definition, usage, and syntax. It covers how to create, access, and loop through arrays, as well as common array methods for manipulation. Additionally, it introduces the concept of nested arrays for multi-dimensional data storage.

Uploaded by

Rajakumar Sai
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Web Technologies Unit-2

Arrays in JavaScript:

What is an Array?
An array is a special variable that can hold multiple values in a single variable.
Each value in an array is called an element.
Elements in an array are stored in a sequential order, and each has a numeric index starting from`0`.

Why Use Arrays?


To store multiple values (like names, numbers, objects) in one place.
To loop through and process lists of data.
To perform common tasks like sorting, filtering, or mapping values.

Array Syntax
let arrayName = [item1, item2, item3, ...];

Example:
let fruits = ["Apple", "Banana", "Mango", "Orange"];

Accessing Array Elements


Use index numbers, starting from `0`.
[Link](fruits[0]); // Output: Apple
[Link](fruits[2]); // Output: Mango

Creating Arrays in JavaScript


1. Using Square Brackets (Most Common)
let numbers = [10, 20, 30, 40];

2. Using `new Array()` Constructor


let colors = new Array("Red", "Green", "Blue");

3. Empty Array and Adding Later


let students = [];
students[0] = "John";
students[1] = "Alice";

Looping Through Arrays


1. Using `for` Loop
for (let i = 0; i < [Link]; i++) {
[Link](fruits[i]);
}

Department of CSM AITAM, TEKKALI


Web Technologies Unit-2

2. Using `for...of` Loop


for (let fruit of fruits) {
[Link](fruit);
}

Common Array Methods

Method Description Example

`push()` Adds item at the end `[Link]("Grapes")`

`pop()` Removes last item `[Link]()`

`shift()` Removes first item `[Link]()`

`unshift()` Adds item at beginning `[Link]("Pineapple")`

`length` Returns array length `[Link]`

`indexOf()` Finds index of item `[Link]("Mango")`

`includes()` Checks if item exists `[Link]("Apple")`

`join()` Joins array to string `[Link](", ")`

`sort()` Sorts array `[Link]()`

`reverse()` Reverses array order `[Link]()`

Modifying Arrays
let numbers = [1, 2, 3];
numbers[1] = 20; // Change 2 to 20
[Link](4); // Add 4 at the end

Nested Arrays (Multi-Dimensional)


let matrix = [
[1, 2],
[3, 4]
];

[Link](matrix[1][0]); // Output: 3

Department of CSM AITAM, TEKKALI

You might also like