Transcript for:
Understanding Control Flow in JavaScript

what is going on guys it's amy you're watching dev dreamer and welcome to lesson 21 in javascript in this lesson we're going to learn all about control flow if you enjoy the content don't forget to like and subscribe down below and choose all notifications by clicking the bell so you never miss an update [Music] okay so welcome back to lesson 21 so in the previous lessons then we've learned quite a lot about different primitive data types built in global objects such as math and date and different kinds of operators what we want to be doing over the next few lessons is learn all about something called control flow so let's take a look at what control flow actually is so by default then our javascript code is executed from top to bottom line by line however we can change this with control flow control flow allows our program to make decisions about what chord is executed and when so this is where javascript really starts to come alive javascript contains certain conditional statements and loops that enable us to control the flow of our code making our projects and applications more powerful more dynamic and more interactive now let's understand all this by looking at a flowchart now you've seen floor charts before i'm sure you start at the top you follow the journey through answer the questions and then as you go along depending on your answers you'll be taken along a certain path until you reach a conclusion and this is basically control flow in action let's take a look at an example okay so for this example then we've got two paths order takeaway or don't order and so hopefully this is going to help us to decide whether we need to order takeaway or not so we start here then with the question are you hungry so this is two possible answers yes or no let's go for no so now the next question is did you eat recently let's say yes we did and in that case then the conclusion is well don't order let's go for a different one let's say are you hungry yes is there any food in the house yes do you want to eat it no so in that case what a takeaway so control flow is all about the order in which our code is run we're able to control the flow of our code and either execute or loop over certain sections based upon whether a given condition is true or false now in so doing what we're doing is we're giving our program the ability to respond to changing conditions and intelligently automate repetitive tasks so of the next few lessons then we're going to be focusing on control flow by looking at javascript's conditional statements and loops let's understand what they both are okay so let's first understand what conditional statements are conditional statements are basically checks to see if a certain condition is either true or false like we had in the previous example with the flow chart the question are you hungry had two possible answers and then if the condition is true we can run say cold a if it's false we run chord b okay so that's conditional statements let's now look at what loops are soul loops allow us to perform repetitive tasks with less cold for example using loops we can iterate over an array and perform some sort of action on each piece of data or we can use a loop to compile a list of all the items let's now take a look at the different conditional statements and loops that we have okay so for conditionals then we have things such as if statements and there's also variance of if statements such as if else and else if we have switch statements and finally we have the ternary operator for loops we have things such as for loops which as we'll see are the most common type of loop in javascript do and while loops for in loops and for of loops so over the next few lessons then we're going to understand javascript control flow by taking each of these in detail we'll start in the next lesson with if statements let's go ahead and summarize so javascript control flow is all about how we control the flow of our code this is done by using javascript's conditional statements and loops and finally using control flow makes our projects more powerful more dynamic and more interactive remember we said that code is executed generally from top to bottom line by line while with control flow based upon whether certain conditions are either true or false we can choose to execute different sections of our code and of course in the coming lessons we're going to see how this all ties together so guys that's it for this lesson in the next lesson we're going to start learning about conditional statements by looking at if statements as always guys if you join the content don't forget to comment share like and subscribe down below and i'll see you on the next one [Music] you