Arrays and Objects

Arrays and Objects

Basics in JavaScript

Arrays are a data structure that stores a collection of elements, which you can grab using a index or a key. This allows you to store values of the same data type under one variable name. Indexing usually starts at 0 for the fist element and then increments of 1.

Here is an example of an array in JavaScript.

let musicalScales = ["C", "D", "E", "F", "G"];

To access these scales inside of the array we would then access the element using indices:

console.log(musicalScales[0]);
console.log(musicalScales[1]);
console.log(musicalScales[2]);
console.log(musicalScales[3]);
console.log(musicalScales[4]);

Lets move on to Objects. Score is set as the object with three key values. These key values belong to score. Lets take a look at what that code embodies.

let score = { // score is the object 
  composer: "Desiah Barnett", // first key-value pair
  title: "Robocop", //second key value pair
  publisher: "Destined Productions", // third key value pair
};

Now lets access the properties (key-value pairs) inside of the object "score". You can do this by using two syntaxes. (Dot Notation or Bracket Notation)

console.log(score.composer); // dot notation
console.log(score.title);// dot notation
console.log(score["publisher"]);// bracket notation

The console.log statement will output the values of the properties.

Desiah Barnett
Robocop
Destined Productions