Now we are going to talk about the order of code execution in JavaScript, which could is going to be executed first, and which which could or a piece of code is going to be executed. Last or next. in JavaScript, the code that is going to be executed first is the code at top of the page. So the code will be the JavaScript will start executing the code from the top to the bottom of the page. So here, what we have is that we have some piece of code here we have console dot log, and then we have a function and then we have another console dot log. So the order of execution in this script JavaScript script is that it's first going to start from line one, nothing and then It goes to line two, nothing, then it's going to go to line three, there is some code, which is console dot log, hi, john.
So it's going to execute that, then it's going to go to line four, nothing. So it's going to move on to line five, nothing again, and then it's gonna, it's gonna go to move on to line six. In line six, there is a function. And functions will never be executed unless you call them. Therefore, in JavaScript, once it finds a function, as we said before, it's not going to be executed. It's just, it's just going to know that there is a function, and it's gonna, of course, move it to the the top of the page, so that whenever you call it, it knows that it knows this function, and it knows what's inside this code.
Therefore, it's gonna just move this code to the top and if Not going to execute it. So it's gonna go through all these lines 678 and nine, and then two, it's gonna go to 10, nothing event 11. And at 11, it's going to execute this code. Now if I save and run this code, what I'm going to get is Hi, john. And then Hi, Kate. So excuted john first, hi, john first, and then it's it has executed Hi, Kate next, but it did not execute this function because functions only get executed when we call them.
So in JavaScript, it starts from the top to the budget to the button and it executes the code at the top and then it moves on. It moves all the way from top to bottom and execute and executes each code it finds. But let's now call the print a print a word function and see what happens. Suppose that I call this function here, Brent Rent a word and move I just pass, Bob. And if now I save on run, I am going to get hi john. And then Hi Bob.
I've been hike it. So this one, this function is going to be executed here, because it was called before the console dot log. But if I call this function after the hi Kate console log code here, if I call it here, what I'm going to get instead is that what I'm going to get is that Hi, john. And then hi Kate, and then Hi, Bob at the button. So it's going to excuted all last. But if I move it, let's move this card from here to the top first.
And if I save and run, what I'm going to get is Hi Bob. And then hi john and then hike it. So in JavaScript, the code gets executed from the top of the page all the way to the bottom. of the page