AstralTurtle / flw1-u2l3-23-24-student-exercises

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Lesson 2.3: Arrays

Arrays: Organizing Your Data

Imagine you have a pile of papers from school, and you need to organize them. That's where arrays come in!

What is a Data Structure?

A data structure is a way of organizing data so that it can be used effectively. Arrays are one such data structure that helps us organize information.

Declaring an Array

In JavaScript, we can declare an array like this:

let numbers = [5, 10, 15, 20, 25];
let fruits = ["Apple", "Banana", "Cherry", "Date", "Elderberry"];
let emptyArray = [];
  • Array elements are listed inside square brackets and separated by commas.

Array Elements

An array element is each individual value in an array. For example:

let artists = ["Beyoncé", "Drake", "Billie Eilish", "BTS", "Ariana Grande"];
  • Array name: artists
  • Array elements: ["Beyoncé", "Drake", "Billie Eilish", "BTS", "Ariana Grande"]

Using Arrays

Arrays are frequently used in websites, apps, and games. Examples include photo galleries, music playlists, shopping carts, and more.

Array Index

An array index is the location of an array element. The first array index is always 0.

Retrieving Elements by Index

You can retrieve a value from an array by using the array name followed by the index number in square brackets. For example:

let pets = ["guinea pig", "gecko", "bird", "cat", "snake"];
let favoritePet = pets[1];
console.log(favoritePet); // This prints "gecko"

Happy coding! 😊

About


Languages

Language:JavaScript 56.6%Language:HTML 34.6%Language:CSS 8.9%