JS-152-Objects-Arrays
  • Introduction
  • 04-Objects
    • OOP-Basics
      • Overview
      • Coding the Example
    • Prototypes
      • Object Prototypes
      • Prototypes Continued
    • Inheritance
      • Inheritance
    • Synopsis
      • Synopsis
    • Challenges
    • Solutions
      • Constructor Function
        • Person Constructor
        • Explanation
      • Inheritance Challenge
        • Code
        • Explanation
      • Family Challenge
        • Child
        • Pet
  • 05-Arrays
    • Array Basics
      • Array Overview
      • Creating an Array
      • Individual Elements and Length
      • Iterating Over Arrays
    • Array Methods
      • Methods Overview
      • .join() Method
      • .reverse() Method
      • .split() Method
      • .replace() Method
      • .splice() Method
      • .map() Method
      • .indexOf() Method
      • .filter() Method
      • .every() Method
    • Palindrome Algorithm
    • Array Challenges
    • Solutions
      • First and Last
        • First Element
        • Last Element
      • Most Frequent
      • Largest and Smallest
        • Single Array
        • Multiple Arrays
      • Missing Number
      • "Arrays" in the DOM
      • Sorting
        • Bubble Sort
        • Selection Sort
    • Resources
Powered by GitBook
On this page
  1. 05-Arrays
  2. Array Basics

Creating an Array

To create an array we use something called bracket syntax. It's more formally known as an array literal or array initializer and looks like this:

var arr = [element0, element1, ..., elementN];

elementN is the list of values for the array's elements.

We'll use an example of an employee list to cover some basic things regarding arrays.

var emp = [];
emp[0] = 'Joe King';
emp[1] = 'Amanda Huggenkiss';
emp[2] = 'Justin Thyme';
console.log(emp);

Your console window should look like this:

[ 'Joe King', 'Amanda Huggenkiss', 'Justin Thyme' ]

Let's discuss the parts of our array:

  • array name - emp

  • elements - emp[0], emp[1], and emp[2]

  • element values - 'Joe King', 'Amanda Huggenkiss', and 'Justin Thyme'

Fun fact: We could have populated our array at the same time we created it!

var emp = ['Joe King', 'Amanda Huggenkiss', 'Justin Thyme'];
console.log(emp);
PreviousArray OverviewNextIndividual Elements and Length

Last updated 7 years ago