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
  • map()
  • slice()
  • splice()
  • File Location
  • Practice
  1. 1-JavaScript Fundamentals
  2. 1-JS-Fundamentals
  3. 9 Arrays

Methods

As mentioned earlier, some of the methods used for strings also work with arrays. Here are some of them:

Method

Function

concat()

Puts multiple arrays together and returns a single array

includes()

Returns true if the given value is found in the array. Otherwise, returns false

indexOf()

Returns the first index location of the given character in the array if present. Returns -1 if not found.

lastIndexOf()

Returns the last index location of the given character in the array if present. Returns -1 if not found.

Here are some others that are commonly used:

map()

Map takes an array, applies a callback function to each element, and returns the result as a new array.

var numbers = [0,1,2,3,4,5,6,7,8,9];
let numbersTwo = numbers.map(x => x * 3) //takes each of the elements of the array and multuplies them by 3
console.log(numbersTwo); //[0, 3, 6, 9, 12, 15, 18, 21, 24, 27]

slice()

Slice makes a copy of a part of the array

var numbers = [0,1,2,3,4,5,6,7,8,9];
console.log(numbers);

//First number is start.
//Second number is end. It will not print the end.
var numbersTwo = numbers.slice(0,3);
console.log(numbersTwo);

//We made a shallow copy, so the 
//original array is still in tact.
console.log(numbers);

var numbersThree = numbers.slice(4,9);
console.log(numbersThree);

splice()

Splice allows elements to be added or removed from an array

let newArray = ['a', 'b', 'c', 'd', 'e']
console.log(newArray); //[a, b, c, d, e]

newArray.splice(0, 2, 'abc'); //first number is the start index, second number is the number of elements to remove (optional), 3rd parameter is the value to place at the start index
console.log(newArray); //['abc', 'c', 'd', 'e']

newArray.splice(3, 1)//removes 1 element at index 3; in this case 'e'
console.log(newArray); ////['abc', 'c', 'd']

newArray.splice(1, 'xyz'); //adds the string 'xyz' at index 1
console.log(newArray); //['abc', 'xyz', 'c', 'd']

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
                03-length.js 
                04-iterating.js 
                05-methods.js <----You will be working in this file.

Practice

Play around with some of the methods above. Come up with a challenge and post it in the slack channel. See how many of your classmate's challenges you can solve.

PreviousIteratingNext10 Objects

Last updated 7 years ago

For additional information, and more built-in methods and functions, see the Mozilla docs

here