Loading
Lesson 11
Courses / JavaScript for Complete Beginners
Introduction to JavaScript Functions

Video Transcript

Welcome back to NVK Tag World. This lesson will be about JavaScript functions. We heard that before, the word function, right? We called the log function from the console object before, and we used that to display some text in the console. So we did what was called a function call. We called upon the log function to display some message to the console. Now we're going to create our own functions. We're going to build them. So a function typically has an input, some input that you provide, and then it does some processing, and then at the end it gives you back some output. So in this case for the log function we gave as input this string, and then it outputted it to the console. Now we have to create our own functions now. Now to create our own functions, we're going to use the keyword function. Okay, and then you give your function a name. Let's say we want to just write a function that will display Hello World, specifically to the console. Let's say the name of the function, I'm going to call it say Hello, and then you have to add some parentheses here, and these will be a list of inputs to this function. But let's say we don't have any input for now, so we can leave it empty. Alright, so you open the braces here, and let's break the line, and I'll put two spaces that by convention for the indentation. And then we're going to do console, log, Hello World. This is the structure this function is going to take. So if you call say Hello, what it's going to do is whatever you write in between the opening and closing braces. This is the body of the function, and it could be one or more statements. In this case it just does the call to the console log, which by the way is yet another function. So you can have functions being called inside other functions that's perfectly fine. And this will log Hello World to the screen. Now when you press enter, you're only going to define this function, but it will not be called. So you're just providing the set of instructions for what's going to happen if you happen to actually type say Hello. If you call say Hello, it's going to do these steps. Now with that enter, we just define the function. Now if you actually want to run that function, you have to say the function name followed by the parentheses. Remember, whenever you see parentheses, you would typically think, oh, function. So what's going to happen when I press enter? Can you guess? So it's going to print Hello World to the console. So let's walk this through. The moment you say this, there's say Hello, that's the name of the function. And then you put the parentheses. Oh, so it says, okay, you want me to call say Hello function. It's going to go here, say Hello to the body and look at all the instructions. And it's going to go one by one from the top to the bottom. You're going to say, okay, you want to log Hello World to the screen. Let's do it. So it did it. And then it reached the end. Nothing else to do. Comes back here. Okay, that's it. Okay. So we created our first function. Now you can call the function multiple times, right? You can do it again and again and again. So it doesn't matter how many times you call it. I was going to do whatever you told it to do. So that's very useful. For example, if we had a program, let's open up my open up text editor here. Suppose I have a program that actually did this very simple thing of logging the message Hello World to the screen. But it did it at multiple points in my program. Not necessarily in order, but it could say Hello World and do whatever. And then after a while, it comes to some part where it does Hello World again and then does something else and then Hello World again. You see, I'm doing the same thing over and over. So if you find yourself doing things over and over again, that might be a good sign that you can actually take that and put it into a function and then just call the function. And the advantage to that is, for example, if I wanted to change the message here, say I don't want to say Hello World anymore every time. I want it to be something else. So if you had to change it, there will be four in this case. Instances where you have to go here, the first one, and change, I don't know, Hello People. And then, okay, I have to change this one too and this one too. And you see, I'm doing so much repetition for the same thing and they all do the same thing. So what you do is actually just define this guy, this statement. You make a function like we did before, say Hello, and then you open the body and you take this guy, and you put that in the function. And then instead of doing this explicitly, just call the function here, every time you need to actually do the console log, I use my text editor or the Atom text editor to select multiple lives, by the way. And you can just say Hello every time you want to say Hello whatever, right? And then you call it here, say Hello, say Hello, say Hello four times. And then if you actually want to change the message, oh, I don't want to Hello People anymore. I want a Hello World. I want it back to Hello World. So you only have to change one line. Only change the function definition here. And then everything else, once you call the function, will call Hello World. So instead of changing every single instance of the console log, you take that, put it into the function, and then you only need to make the change to the function and nowhere else. Okay, so that's one of the advantages of using a function. Okay, now let's take a deep breath and go back a key here to the console to review. So previously we had now, we learned how to call a function, we called the log function from the console object to print a message to the console. Now we wanted to create our own functions or all set of instructions to be called every time you need something done. And we created a function called say Hello. You put the keyword function before name the function and some parentheses. This is going to be the argument list. Okay, the parameters of this function takes, but in this case there's nothing. So it doesn't really take any input, but it does produce some output, which is the console log instruction here that we added. So if you want to call a function, all you have to do is the name of the function followed by the parentheses. Okay, in this way, and it will do whatever instructions were you told it to do in the definition to the function here in the function body between the curly braces. Remember, when you type the function definition and you type enter, it does not actually call the instructions, it just defines the function. If you want the instructions to be executed, you have to call the function by its name followed by the parentheses. Okay. So that's a very brief introduction to the function in JavaScript. And I hope you like this video. Please like and subscribe and I'll see you next time. All right.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: