Skip to content

Latest commit

 

History

History
55 lines (32 loc) · 1.49 KB

File metadata and controls

55 lines (32 loc) · 1.49 KB

Lesson 5.1: 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! 😊