2.8 Functions Types
Hello everyone and welcome to this course where we're going to look at function types in JavaScript, there's a cleaner way to write this code on line 3. All of these concatenations are not very clean. Later in the course I'll show you how to use templates to clean up this code. For now, don't worry. Let's look at another example of a function. This function we have here performs a task, so we're going to write in comment //performs a task. This task is to display something on the console. But sometimes our functions can calculate something. So here is an example of a function that calculates a value. So we will call once again a function, we will call it function square. This function must take a parameter, we'll call it "number". Now we have to calculate the square of its number, that is number * number. Then we have to return this value to the caller of this function, for this we use the keyword "return" it's a reserved word so we can't have variables called "return". Instead of calling the host function, we call the function carrer. Pass the value 2 in parentheses. And this returns a value so we can use this value to initialize a variable. For example you can declare another variable call number and set it to a square of 2, we display this on the console. We save the changes, we get 4 on the console. Now in this particular example we don't necessarily have to declare a separate variable if all we want to do is display the square of 2 on the console. We can exclude this variable declaration and simply pass the square of 2 to the console.log. That way, when the JavaScript engine executes this code, it will first call this function and it will get a value to pass to the console.log Save the changes and look, we still get 4. Now how many function calls do you think we have in this code? We have two function calls because 2 is a function call but console.log is also another function call. Because here we have parentheses, we have the log function that defines somewhere and by passing an argument we can pass a simple string like "hello". Or we can pass an expression, that expression can be a call to another function like square of 2. So that's the basics of functions in JavaScript, later on in the course we have a complete section on functions. For now, all I want to remember is that a function is a set of instructions that performs a task that calculates and returns a value. A real application is essentially a collection of hundreds or thousands of functions that work together to provide the functionality of that application. That's it for this video on function types in JavaScript, let's meet on the next section where we'll see the different operators in JavaScript.