Topics
- For Loops
- Arrays
- Functions
For Loops
The For Loop will repeat until a specified condition is false. What happens when a for statement is running?
- The variable section is executed; this expression usually sets a counter for the loop to declare a specific value.
- The conditional section is evaluated; If the value is true, the loop will continue to run. When it is false, the for loop stops. If the conditional statement is omitted, the default is true. This may also lead to an infinite loop. No bueno.
- The statement executes.
- The increment section is executed. In this, you can designate whether the value is added, subtracted, counts in increments, etc.
for (var i = 0; i <= 50; i += 5){
console.log(i);
This is read as, "Variable i starts at 0, we are counting up to 50, and we are counting to 50 by 5's."
Arrays
An array is a numerically indexed map of values. Starts at 0. Here are some key points:
- An array is created like a variable.
- It can be named anything.
- Uses square [] brackets
- Uses commas in the brackets to separate items.
- Can have a collection of one type OR can collect multiple types.
let colors =["Red", "Green", "Blue", "Purple", "Orange"];
console.log(colors[3]);
In this console.log, we are calling out index value 3, which would give us a result of Purple.
Functions
A function allows us to execute some action or actions. A few key parts to functions:
- Calling a function: means we are going to use it; say the name of the function followed by ().
- Parameters: the names listed in the function definition
- Arguments: the real values received by the function when you call it.
- The "RETURN" keyword: the return is the value that the function spits out.
function hi() {
console.log('hello!');
}
hi();
Here we have named the function "hi", and want to give it the result "hello!" when we call it.