CN-Marcus / flw1-u2l5-23-24-student-exercises

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Lesson 2.5: Array Properties & Methods

๐Ÿ—‚๏ธ Array Properties & Methods

Recap: What is an Array?

An array is a data structure that can store multiple values in a single variable. Each item in an array is called an element, and elements are ordered and accessed using a numerical index.

let fruits = ['apple', 'banana', 'orange'];

Accessing Array Elements

Array elements can be accessed using their index, starting at 0 for the first element.

let fruits = ['apple', 'banana', 'orange'];
let firstFruit = fruits[0]; // 'apple'

Array Properties

Arrays have built-in properties that provide useful information about the array, such as the length property, which gets the number of elements in the array.

let fruits = ['apple', 'banana', 'orange'];
let numFruits = fruits.length; // 3

Array Methods Overview

Array methods are built-in functions that allow you to perform various operations on arrays easily. Here are some common array methods:

  • push(): Adds elements to the end of an array.
  • pop(): Removes the last element from an array.
  • shift(): Removes the first element from an array.
  • unshift(): Adds elements to the beginning of an array.
let fruits = ['apple', 'banana', 'orange'];
fruits.push('grapes'); // ['apple', 'banana', 'orange', 'grapes']
fruits.pop(); // ['apple', 'banana', 'orange']
fruits.shift(); // ['banana', 'orange']
fruits.unshift('kiwi'); // ['kiwi', 'banana', 'orange']

๐Ÿ”„ DOM Manipulation Review

While itโ€™s important to access data from the array and modify it, we also want to display that data on our page!

DOM Manipulation

  • createElement(): Creates an HTML element in JavaScript, but it's not yet in the DOM.
  • appendChild(): Attaches an element to the DOM, adding it as the last child.

Example:

let para = document.createElement("p");
para.innerHTML = "Hello!";
document.body.appendChild(para);

Happy coding! ๐Ÿ˜Š

About


Languages

Language:JavaScript 64.3%Language:HTML 21.2%Language:CSS 14.5%