<!DOCTYPE html>
<html>
<head>
<title>Week 7 - Arrays, Objects and Loops</title>
</head>
<body>
<h1 id="headline">Storing Data in JavaScript</h1>
<ul id="list"></ul>
</body>
</html>
Essentially, everything in JavaScript is an object. You can create object when you need to store multiple variables to a particular place.
A variable that provides information about an object. Needs to be paired with a value.
A way of assigning multiple values to one variable.
Allows you to "loop through" each object in an array and perform a function on each.
An array is a way of assigning a series of values to a single variable. Each object in an array is separated by commas.
Each object in an array is assigned an index value, starting with 0.
You can use the length property of an array to see how many items are in it. For example: arrayName.length;
Or using the array from the previous example, fruits.length, which will return 4.
To grab an item from an array, you simply need to refer to the variable holding the array, and then the item's index.
var myFruit = fruits[2];
A way of storing multiple pieces of information in a single place.
When storing variables to an object, you need to specify both the property and its value.
An object can also contain a function that let's you interact with the object's properties. This is called a method.
myCountry = {
getCapital: function() {
alert(this.myCapital);
},
myName: "USA",
myCapital: "Washington, D.C."
};
To call this method: myCountry.getCapital()
Essentially everything in javascript and jQuery is an object.
Arrays, functions, elements, strings, etc. And every element has properties and methods.
Create your own object on your index page with three different properties and store in a variable called book. Use your favorite book and define the title, author, genre
Arrays and objects can be part of each other. You can have an array of objects, and arrays can be values in objects.
var books = [
{
title: The Thornbirds,
author: Colleen McCullough,
genre: Fiction
},
{
title: The Price of Inequality,
author: Joseph Stiglitz,
genre: Economics
},
];
A way of cycling through an array and performing an action on each object.
There are multiple types of loops, and using other JavaScript frameworks, multiple ways of performing them. We will focus today on the for loop, as it will likely be the most used and is the basis for the each method in jQuery.
Each loop has four parts that need to be defined
myArr = ["one","two","three","four"];
for ( var i=0 ; i < myArr.length ; i++){
alert(myArr[i])
}
In JS, the lowercase i represents the index of an object in an array. In loops, we use i to represent each index.
...before we build our first application.
A return statement determines the value the function produced or returns. return passes an object to the code that called your function.
In your function, say return variableName; to define what you want to pass back to your original code.
Used to assign values, compare values, perform arithmetic operations, and more. You've already use them to define variables and to concatenate strings. You can also add (+), subtract (-), multiply (*) and divide (/) numbers using usual arithmetic rules and order of operations.
To perform further mathematical operations on numbers, JavaScript has a number of Math operations available, which allow you to round numbers, find the largest or smallest number in an array or return a random number.
Example: Math.max(12,35,5,67,22.3,9); will return 67.
Stands for Not-A-Number in JavaScript. You may see this returned on one of your Math or arithmetic operations, indicating that you are working with variables that aren't numbers, and which can't be operated on.
You can also evaluate the value of variable to check if it is a number or not, using isNaN();.
You can find them on my GitHub page.