[Go to site: main page, start]

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

Arrays in JavaScript

Arrays in JavaScript are collections of items stored in a single variable, allowing for multiple values of various data types. They utilize zero-based indexing for accessing and modifying elements, and common methods such as push, pop, map, and filter simplify array manipulation. Looping through arrays is crucial for displaying or processing their contents.

Uploaded by

reyepe9070
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 views4 pages

Arrays in JavaScript

Arrays in JavaScript are collections of items stored in a single variable, allowing for multiple values of various data types. They utilize zero-based indexing for accessing and modifying elements, and common methods such as push, pop, map, and filter simplify array manipulation. Looping through arrays is crucial for displaying or processing their contents.

Uploaded by

reyepe9070
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

Arrays in JavaScript

An array is a collection of items stored in a single variable. It lets you store multiple
values — like a list of names, numbers, or even other arrays.

1. Creating Arrays

let fruits = ["apple", "banana", "mango"];


let numbers = [10, 20, 30, 40];

You can mix data types, but it’s better to keep arrays consistent.

let mixed = [1, "hello", true];

2. Accessing and Modifying Elements

Arrays use zero-based indexing.

let fruits = ["apple", "banana", "mango"];


[Link](fruits[0]); // "apple"
fruits[1] = "orange"; // change "banana" to "orange"
[Link](fruits); // ["apple", "orange", "mango"]
3. Array Length

let colors = ["red", "green", "blue"];


[Link]([Link]); // 3

4. Common Array Methods

a. push() – Add item at the end

[Link]("yellow");

b. pop() – Remove last item

[Link]();

c. shift() – Remove first item

[Link]();

d. unshift() – Add item at the beginning

[Link]("pink");
5. Looping Through Arrays

Using for loop:

let fruits = ["apple", "banana", "mango"];


for (let i = 0; i < [Link]; i++) {

[Link](fruits[i]);
}

Using for...of loop:

for (let fruit of fruits) {


[Link](fruit);
}

6. map() – Transform Array

Creates a new array by applying a function to each item.

let numbers = [1, 2, 3];


let doubled = [Link](num => num * 2);
[Link](doubled); // [2, 4, 6]

7. filter() – Filter Items

Returns a new array of items that match a condition.

let scores = [30, 50, 90, 20];


let passed = [Link](score => score >= 50);
[Link](passed); // [50, 90]
8. Other Useful Methods

Method Description

includes() Checks if an item exists in the array

indexOf() Finds index of an item

slice() Extracts part of the array

join() Joins array into a string

Example:

let names = ["Ali", "Sara", "John"];


[Link]([Link]("Sara")); // true
[Link]([Link]("John")); // 2
[Link]([Link](" - ")); // Ali - Sara - John

Summary

• Arrays store lists of data in one variable.


• Use indexing to access or update items.
• Common methods like push , pop , map , filter make array handling
easier.
• Looping is essential for displaying or processing lists.

You might also like