krooks12 / 5two

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Lesson 5.2: Array Methods & Properties

Overview

Arrays in JavaScript are used to store multiple values in a single variable. They are a fundamental aspect of managing collections of data.

Array Properties

  • Length: Determines the number of elements in an array.
    const arr = [1, 2, 3];
    console.log(arr.length); // Output: 3

Manipulating Arrays

Adding Elements

  • push(): Adds one or more elements to the end of an array and returns the new length.

    arr.push(4); // Adds 4 to the end
  • unshift(): Adds one or more elements to the beginning of an array and returns the new length.

    arr.unshift(0); // Adds 0 to the start

Removing Elements

  • pop(): Removes the last element from an array and returns that element.

    arr.pop(); // Removes the last element
  • shift(): Removes the first element from an array and returns that element.

    arr.shift(); // Removes the first element

This guide covers the basics of array manipulation in JavaScript, focusing on commonly used methods and properties. Understanding these will significantly aid in data handling and manipulation tasks in your projects. Practice these methods to become proficient in managing arrays efficiently.

Happy coding! 😊

About


Languages

Language:CSS 45.6%Language:JavaScript 41.8%Language:HTML 12.6%