Arrays Hold multiple values in comma-separated list
JAVASCRIPT : BASICS II
// Arrays can hold a mix of data types
let numArray = [13, 36, 45, 57, 14];
let mixedArray = ["a", "b", 1, 2, 3];
Objects Store multiple key : value pairs, or properties
const book = {
.length Property tells us the length of an array
title: "The Hobbit",
let fruits = ["apple", "banana", "orange"]; author: "J.R.R. Tolkien",
// Assign array length to a variable year: 1937
const numFruits = fruits.length; };
console.log(numFruits); // Output: 3
Dot Notation Used to access, add, & update object properties
// Access a property
Array Methods Used for editing array elements
console.log(book.title);
// Adds element to end of array // Output: The Hobbit
fruits.push("pear");
// Assign a new property
// Adds element to beginning of array book.edition = "1st Edition";
fr
uits.unshift("strawberry");
console.log(book);
// Removes and returns the first element /* Output: {
fr
uits.shift(); title: "The Hobbit",
year: 1937,
// Removes and returns the last element author: 'J.R.R. Tolkien',
fr
uits.pop(); edition: '1st Edition'} */
// Returns the index of the value // Update a property
fr
uits.indexOf("apple"); book.edition = "2nd Edition";
// Returns true or false if value in array console.log(book);
fr
uits.includes("dragonfruit"); /* Output: {
title: "The Hobbit",
year: 1937,
author: 'J.R.R. Tolkien',
Function Keyword used to define a function edition: '2nd Edition'} */
// JS function names are in camelCase
function myFunction() {
Object Methods Function defined as properties
console.log("Hello, World!");
} const dog = {
myFunction(); name: "Benny",
sound: "woof",
// Method returns a string
Return Keyword sends value back from a function makeSound() {
return dog.name + " says " + dog.sound + "!")
function myFunction() { }
return "Hello, World!"; };
}
// Will not appear on console console.log(dog.makeSound());
myFunction(); // Output: Benny says woof!
Made with by