JS-101-Fundamentals
  • JavaScript Library
  • 1-JavaScript Fundamentals
    • 0-Getting Started
      • Welcome
    • 1-JS-Fundamentals
      • 1-Grammar and Types
        • Comments
        • Declarations
        • Scope
        • Hoisting
        • Types
        • Literals
      • 2-Control Flow and Error Handling
        • if
        • if else
        • switch
        • try catch
        • throw
      • 3-Loops
        • For Loops
        • For In Loops
        • For Of Loops
        • Do While
        • While Loops
      • 4-Functions
        • Declarations
        • Expressions
        • Calling Functions
        • Scope
        • Parameters
        • Arguments
        • Closures
        • Closures Challenge
        • Arrow Functions
        • Predefined
      • 5-Expressions and Operators
        • Assignment
        • Comparison
        • Ternary
        • Typeof
        • Left Hand Side
        • Spread
      • 6 Numbers and Dates
        • Numbers
          • Numbers Enhanced
        • Math
        • Dates
      • 7 String Methods
        • String Literals
        • Methods
      • 8 Regular Expressions
        • Basic Intro
      • 9 Arrays
        • Array Review
        • Populating/Referring
        • Length
        • Iterating
        • Methods
      • 10 Objects
        • About Objects
        • Properties
        • Enumeration
        • Initializers
        • Constructor Functions
        • this
        • create
        • Methods
      • 11 ES6 Intro
        • ES6 Intro
        • let
        • const
Powered by GitBook
On this page
  • Populating an array
  • Referring to elements in an array
  • File Location
  • Practice
  1. 1-JavaScript Fundamentals
  2. 1-JS-Fundamentals
  3. 9 Arrays

Populating/Referring

This module will teach you how to add values to an array and refer to those values within functions. There will be other methods available later than the ones discussed here.

Populating an array

Previously, you've seen how to add values to an array when it is created. There are a few methods available to add or remove additional elements to the array later:

Method

Purpose

pop()

Removes the last element from the array. Other values are not affected

push()

Adds the given value to the end of the array. Other values are not affected

shift()

Removes the first element from the array. Other values have their index decreased by 1

unshift()

Adds the given value to the beginning of the array. Other values have their index increased by 1

Here are a couple of examples:

let newArray = ["green", "blue", "yellow"];
newArray.push("purple"); //['green', 'blue', 'yellow', 'purple']
newArray.pop("purple"); //['green', 'blue', 'yellow']

newArray.unshift("red"); //['red', 'green', 'blue', yellow']
newArray.shift("red"); //['green', 'blue', 'yellow']

Referring to elements in an array

When referring to items in an array, the index of the item is used to pull out the value inside of it. To use the previous example, newArray[0] would return "green". You can replace the value of an element by manually declaring a new value:

newArray[1] = "white"; //['green', 'white', 'yellow']

There are methods available to target specific indexes of arrays to insert, remove, or modify the contents. We'll expand futher on those in a later module.

File Location

We will be working in the following file:

    javascript-library
        └── 0-PreWork
        └── 1-Fundamentals
            └── 1-Grammar-and-Types
            └── 2-ControlFlow-and-ErrorHandling
            └── 3-Loops
            └── 4-Functions
            └── 5-Expressions-and-Operators
            └── 6-Numbers-and-Dates
            └── 7-StringMethods
            └── 9-Arrays
                01-array-review.js
                02-populating-referring.js <----You will be working in this file.

Practice

  1. Create 4 arrays of 4 elements each. Print to the console the first element of the first array, the second element of the second array, the third element of the third array, and the fourth element of the fourth array.

  2. Create a blank array. Use the methods above to add and remove items from the array. Print the contents of the array after each action.

PreviousArray ReviewNextLength

Last updated 7 years ago