Code Monkey home page Code Monkey logo

2-14-24's Introduction

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! 😊

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.