Variable Declaration and Constant
1 2 3
myMath in this case is the sum of variables x and y, which would be equal to 40.
To find out what data type a variable is, you use the
typeof method, as shown below:
1 2 3 4 5 6
You can also use an if/else statement to execute a given statement if a certain condition is true. For example, if you wanted to check and see if a variable was considered a type of string, you’d do the following:
1 2 3 4 5 6
Local vs. Global Scope
1 2 3 4 5 6 7 8 9 10 11 12
The x and y variables that are outside of the add and subtract functions are called
global variables. They are declared outside of a function in the global scope, and are essentially accessible from anywhere inside of the program.
Functions create a new scope. The x variable inside the add function and the y variable inside of the subtract function are known as
local variables. They exist only in the context of the respective function, and are not accessible outside of the function. Think of it as the Vegas motto; what happens here, stays here.
Making a list inside of our app would involve the following:
Looping Through Arrays
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
This is just a simple looping function that takes a roster, and adds a name to the roster array. If the array does not have any elements in it, then it will push that element onto the end of the array, which will be the first element. Otherwise, for each element in the array, if an array position is undefined, then it will set that element equal to the name value. Also, if it is the last element in the array, it will also push the name value to the end of the array. Finally, once the whole array has been iterated through, it will return the array.
These are mostly for me to remember and repeat. I’m currently branching out into other languages at the moment, and will be focusing on JS, jQuery, and Ajax.