If/Else Flow Control0:00
I feel like I'm a broken record because I keep saying that there are a lot of similarities between php and JavaScript, and we're going to continue that because here we're going to discuss flow control, you know, making decisions with if statements and then using loops. So we know that we have if statements, we know we have else statements. We also have else if statements if we need to perform some other operation if the first one fails, and we can have as many of those as we need. So there's really no need to go on from there because it's exactly what you would expect. So we have ifs, we have else ifs, we have else, there we go. So next we have the ternary operator, which we could check if one is greater than three.
Ternary and Switch0:37
So we have ifs, we have else ifs, we have else, there we go. So next we have the ternary operator, which we could check if one is greater than three or one is less than three, then we can say less than, which would be assigned to the value variable, or if that was false, then we would have greater than. So same thing as PHP, there's nothing to really discuss. So we can move on to the good old switch statement. So first of all, we need a value. Let's have something like hello so that we can switch on that value, then we will have our cases. If it's hello with a capital H, then we would want to do something, then we would break,
Basic Loop Structures1:13
our cases. If it's hello with a capital H, then we would want to do something, then we would break, then we would have a case for, let's say, world, and we can keep going on and on and on until we have the default, and it's really hard to type and talk at the same time. So we can have that, and there we go, JavaScript, PHP, control structures are very similar. Really the main differences are going to be in our loops, but the basic loops like the for loop is going to be exactly the same. So we will start off with the initialization, then we will have our condition, and then finally our increment. So that if we wanted to write that out to the console, we could do that very easily,
finally our increment. So that if we wanted to write that out to the console, we could do that very easily, except that we need two plus signs there, so that in the browser we see each one of those values in the console. So then we have the while loop, which works exactly like you would think. So while will say i is less than 10, and we will do the same thing. We will write out that value, but we do need to increment our counter so that we don't get into a never-ending loop. We see the same result, so we can move on. We also have the do while, so that we can write out our value to the console while our
We see the same result, so we can move on. We also have the do while, so that we can write out our value to the console while our counter is less than 10. But once again, we do need our increment. So after we write the value to the console, we will increment that so that in the browser we get our same result. But a lot of times if we are using a loop, we are iterating over an array. And we can of course use any of the loops that we have talked about, but we also have a loop specifically for iterating over an array if all we need to do is work with the individual elements in that array.
Iterating Arrays with for...of2:59
a loop specifically for iterating over an array if all we need to do is work with the individual elements in that array. So let's create an array called fruits, so that we will have an apple, let's have a banana, let's also have an orange, and let's do cherry, a cheery cherry. And we want to iterate over these, so we're going to use a for...of loop, which is going to give us the individual value, which I'm going to call fruit of, and then the name of our array, so fruits. So it's for var fruit of fruits, and we're going to write each fruit out to the console. So that's whenever we refresh the page, we see our fruit, apple, banana, orange, and cherry. So of course, this loop is very useful if we don't need the index or anything like that.
Iterating Objects with for...in3:40
So that's whenever we refresh the page, we see our fruit, apple, banana, orange, and cherry. So of course, this loop is very useful if we don't need the index or anything like that. We just need to work with the individual values from an array. But we also have a loop specifically for iterating over the keys inside of an object. Now, we haven't necessarily talked about objects yet, but it's relatively straightforward. It's just a data structure that gives us a set of key value pairs. So I'm going to create an object called person. That object is going to be defined using object literal notation. And we're going to have a firstName, which is a property. We'll have a colon, and then the value that we want to give that property.
And we're going to have a firstName, which is a property. We'll have a colon, and then the value that we want to give that property. So it's a little bit different than what you might expect. Now, whenever we talk about objects, we will talk about other ways to create them. This is just object literal notation, which is very common. So we have our property name, the colon, and then the value. We also need the lastName, colon, and then the value. So this is a person object to represent John Doe. And if we want to iterate over these properties, we can use a for in loop, which iterates over the properties.
And if we want to iterate over these properties, we can use a for...in loop, which iterates over the properties. So we are going to get the property in the object, and we can do this. We will output the property name, and then we want to output the value, which is going to look like we are using this person object as an associative array, because we have this square bracket syntax. But if you think about it in PHP, an associative array is really just a set of key value pairs. Well, in JavaScript, we don't have an associative array, but our objects are, well, it's a set of key value pairs.
Using Break and Continue5:25
Well, in JavaScript, we don't have an associative array, but our objects are, well, it's a set of key value pairs. So it's very similar, at least in concept. And if we take a look at this in the browser, we can see that we have the firstName property, and its value is John, and the lastName property, and its value is Doe. And then finally, we have two keywords. So let's check. If prop is firstName, then we want to go ahead and output that to the console, but we want to stop iterating over the properties. So we can call break, which breaks out of the loop.
output that to the console, but we want to stop iterating over the properties. So we can call break, which breaks out of the loop. And of course, if we have break, that means that we should have continue, which we do. So now, if the prop is firstName, we're not gonna do anything. We are just going to continue, so that all we should see is lastName in the console. So control structures are largely the same between php and JavaScript. Of course, JavaScript has slightly different loops for working with arrays or objects.
Of course, JavaScript has slightly different loops for working with arrays or objects. But as far as making conditions, or even just the basic loops, those are all practically the same. But that is not going to be the case in the next episode when we talk about functions.
