Arrays
• Arrays are like lists. They are a data structure that helps to organize information.
• Arrays store multiple items together. Strings must be individually wrapped with quotation marks.
• Arrays are indicated by outer [ ] brackets and can contain any value of integer or string, and usually only contain one data type per array.
• Arrays can contain multiple data types.
• Separate elements in an array by using a comma.
example: myArray = [“banana”, “apple”, “plum”]
• Each element in an array is stored in a sequential order. Index numbers are applied to the elements starting from 0 on the left and counting up in whole number steps from left to right.
• Access an element in an array by calling the arrayName then indicating the index number(s) you want to call.
example: console.log(arrayName[1]);
• Change an element in an array by simple writing in a new variable to the list item.
example: arrayName[0] = “new value”;
• Use the .length property to check how many elements are in an array list.
example: console.log(myList.length);
PUSH AND POP (In arrays)
• Push and pop are array methods that adds to or removes an item from the end of an array, respectively.
examples: arrayName.push(elementToAdd);
arrayName.pop();
Objects
• Objects can store multiple types of information (Booleans, arrays, strings, numbers or even other objects!), in contrast to an array which can only store a list or elements that have shared characteristic.
example: const aboutMe = {
name: "Lea Thomas",
hometown: "Haiku, Hawaii",
age: 29,
isAMusician: true,
hobbies: ["picking wildflowers", "playing guitar", "reading poems"]
};
KEY-VALUES
• In the example above, the object is aboutMe and it contains five key-value properties.
• A key-value property is something written as a pair of data. The keys and values are separated by a colon and commas are used at the end of each line to separate each new pair of key-values.
name: “Lea Thomas”,
hometown: “Haiku, Hawaii”,
DOT NOTATION
• call upon a specific property by the name of the key
console.log(aboutMe.age) //// this would return 29
CONCATENATING USING OBJECT ELEMENTS
• Concatenation is the act of combining multiple elements.
• Can be comprised of variables, strings, numbers, object or array indexes
• use a + to combine the elements and don’t forget to add a space where necessary in the string quotations.
console.log("Hello! My name is " + aboutMe.name + "and I am " + aboutMe.age + " years old.")
• Subract operator only knows how to subtract so it will sometimes auto-correct a string number to a real number without being dictated to do so.
• Addition can concatenate elements but also serve as an arithmetic operator. If you try to add string + number, it will give you an error, often “NaN” which stands for Not a Number.
• To be specific about when to add numbers, use functions: Number(myStrVariable) to convert a “string number” (“10”) to a plain number 10. Or toString(myNumVariable) to convert a number to a string.