Objects
In JavaScript Variables are containers for data values. Objects are variables too except objects can contain many values. Let's look at an example below:
var person = {
firstName:"John",
lastName:"Doe",
age:50,
eyeColor:"blue"
};
Arrays
We just learned a little about objects. Arrays are "list-like objects"; they are basically single objects that contain multiple values stored in a list. If we didn't have arrays, we'd have to store every item in a separate variable. Let's look at an example below:
var sequence = [1, 1, 2, 3, 5, 8, 13];
for (var i = 0; i < sequence.length; i++) {
console.log(sequence[i]);
}
For Loops
Loops are used to run the same bit of code over and over again, each time with a different variable. For loops loop through a block of code a number of times. Let's look at a For Loop that adds all numbers 1-50.
let sum = 0;
for (let i = 1; i <= 50; i++) {
sum = sum + i
}
console.log(sum)
If Else Conditionals
the If Else executes a statement if a specified condition is truthy. If the condition is falsy , another statement can be executed. Let's look at an If Else statement that looks at the temperature and determines if we should wear a jacket.
let temperature = 75;
if (temperature < 70) {
console.log('Wear a jacket!');
} else {
console.log('No jacket necessary!');
}
Functions
A function is code that can be called by other code or by itself. When a function is called, arguments are passed to the function as input, and the function can optionally return an output. This is also an object. Let's look at a basic function.
let array = ['This', 'is', 'really', 'cool'];
function cool() {
console.log('This')
console.log('is')
console.log('really')
console.log('cool')
}
cool()
Switches
A Switch is another type of conditional. It is very similar to If Else statements. It uses cases and breaks instead of Ifs and Elses.
switch (dessert) {
case "pie":
console.log('Pie, pie, me oh my!')
break;
case "cake":
console.log('Cake is great!')
break;
case "ice cream":
console.log('I scream for ice cream!')
break;
default:
console.log('Not on the menu')
}