본문 바로가기
TIL: Today I Learned

[TIL] 20201130 JavaScript Arrays

by 김알리 2020. 12. 1.

Data Structure

A collection of data

 

 

 

Arrays

  • Ordered collections of values.
  • Examples
    • List of comments on IG post
    • Collection of levels in a game
    • Songs in a playlist
  • Ways to Create Arrays
const arr = [];
const strArray = ["orange", "red"];
const numArray = [1, 2, 3];
const mixedArray = [true, "orange", 32];

 

 

 

Array Random Access

  • Array Index
    • Each element has a corresponding index, counting starts at 0.
    • Example
const num = [1, 2, 3, 4];
num[2]; //3

 

 

 

Array Methods

  • push : add to end
  • pop : remove from end
  • shift : remove from start
  • unshift : add to start
  • concat : merge two or more arrays
  • indexOf : returns the first index at which a given element can be found in the array, or -1 if not found.
  • includes : determines whether an array includes a certain value among its entries, returning boolean value.
  • reverse : reverses an array in place. The first element becomes the last, the last the first. Destructive to the original.
  • slice : copy a portion of an array
  • splice : changes the contents of an array by removing or replacing existing elements and/or adding new elements in place
  • sort : sorts the elements of an array in place and returns the sorted array.

 

 

Reference Types & Equality Testing

  • Even same-looking arrays are not double nor triple equal, because JavaScript compares references in memory.
  • If two array variables have the same reference, one changes together when the other one changes.

 

 

Arrays & Const

  • The values of the array can change even when const is used, as long as the reference remains the same.
  • Use const with arrays so that random new reference can't be assigned. It's a safety precaution.

 

 

Multi-Dimensional Arrays

  • Nested Arrays : Arrays can be stored inside of other arrays.

 

 

 

 

 

 

* This post is a summary of Udemy Course "The Web Developer Bootcamp" by Colt Steele.