Lessons: 65Length: 7.1 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.8 Functions

Functions are the cornerstone of JavaScript. Let’s look at some basic features of functions.

Syntax

  • function
  • function(parameters)
  • return

1.Introduction
2 lessons, 07:42

1.1
Introduction
02:12

1.2
Setup
05:30

2.Language Fundamentals
8 lessons, 1:00:53

2.1
Variables
06:33

2.2
Data Types
11:28

2.3
Arithmetic, Assignment, and Comparison Operators
10:24

2.4
Unary, Logical, Comma, and Spread Operators
09:02

2.5
Operator Precedence
03:50

2.6
Reserved Words
04:17

2.7
Strict Mode
04:34

2.8
Functions
10:45

3.Data Structures
5 lessons, 22:52

3.1
Arrays
04:29

3.2
Objects
04:30

3.3
Sets
04:57

3.4
Maps
04:21

3.5
Weak Maps and Weak Sets
04:35

4.Controlling Program Execution
7 lessons, 37:06

4.1
Conditionals
07:49

4.2
Switch Statements
04:41

4.3
The For Loop
06:39

4.4
The `for .. in` Loop
05:17

4.5
The `for .. of` Loop
04:02

4.6
Iterators
05:03

4.7
While Loops
03:35

5.Using JavaScript
13 lessons, 1:44:36

5.1
Working With Strings
09:32

5.2
Template Literals
05:46

5.3
Working With Numbers
06:57

5.4
Working With Arrays
12:53

5.5
Iterating and Transforming Arrays
07:33

5.6
Working With the Object Type
13:55

5.7
Object Literal Extensions
06:45

5.8
Working With Object Instances
06:45

5.9
Getters and Setters
05:00

5.10
Custom Objects
11:28

5.11
The `Math` API
04:54

5.12
Working With Dates and Times
08:10

5.13
The `Array` Constructor
04:58

6.Functions
8 lessons, 56:07

6.1
The `this` Object
06:15

6.2
Working With Functions
10:11

6.3
Scope
07:37

6.4
Arrow Functions
06:59

6.5
Generator Functions
08:13

6.6
Closures
05:00

6.7
Prototypes
06:26

6.8
Default and Rest Parameters
05:26

7.Miscellaneous
6 lessons, 52:39

7.1
Destructuring Assignments
08:09

7.2
AJAX
08:30

7.3
Regular Expressions
10:51

7.4
More About Regular Expressions
08:38

7.5
Classes
06:48

7.6
ES Modules
09:43

8.Working With the DOM
6 lessons, 37:39

8.1
Selecting HTML Elements
05:02

8.2
Manipulating HTML Elements
07:40

8.3
DOM Traversal
05:25

8.4
Adding and Removing Elements
04:45

8.5
Creating Elements and Other Nodes
04:39

8.6
DOM Events
10:08

9.Web APIs
4 lessons, 17:41

9.1
The Selector API
03:03

9.2
Geolocation
05:29

9.3
Web Storage
05:24

9.4
Web Workers
03:45

10.Asynchronous JavaScript
5 lessons, 26:23

10.1
Promises
09:52

10.2
Promise Chaining
05:11

10.3
The async Keyword
03:21

10.4
The await Keyword
04:04

10.5
More About async and await
03:55

11.Conclusion
1 lesson, 00:43

11.1
Conclusion
00:43


2.8 Functions

Hi folks. In this lesson, we are going to take a look at the general work course of our JavaScript applications, the function. A function is a named grouping of one or more expressions. A function can be invoked at anytime to execute all of the statement inside the function. We'll use functions a lot, and they can be used in a variety of different ways. So it's important to understand them fully. The most basic way to find a function is using the function keyword followed by the functions identifier, followed by opening and closing parentheses, followed lastly by opening and closing curly brackets like this. So just to recap, we have the function keyword, followed by the functions identifier, which in this case is function one, the parenthesis are the parameter list. In this case, the function doesn't receive any parameters, because the parenthesis are empty. And lastly, all of the expressions within the function are contained within curly brackets. In this case the function doesn't have any expressions inside of it, but that doesn't make it any less of a function. Let's just add an expression however inside of it. So this form of defining a function is called a function declaration. To invoke this function, we use its identifier followed by a single set of parentheses. Like this. And we can take a look at that in the browser. And we should see the log message that we added within the function. If we want to pass values into the function, which are treated as variables inside the function, we can list them as parameters in the function declaration. So in this case, we have specified that function one should accept a single parameter, which is called msg. And inside the function we just log whatever is parsed as that parameter to the browser's console. And then following this, we can invoke the function any number of times that we want, and we can pass in a different string each time. And whichever string we pass in will get logged to the browser's console. So this is very useful, because it makes the function reusable. We pass different data into the same function and get different results. There is also a subtle distinction to be made here. When we declare the function, we list the parameters that the function accepts. When we invoke the function, we pass it arguments. This is the difference between parameters and arguments, but you will very often hear them used interchangeably. We can list as many parameters in the parentheses of the function as we want by listing multiple parameters separated by a comma. So these parameters that we have specified as part of the function declaration are named parameters. And named parameters are made available inside the function, as if they were variables. Functions have access to another special value inside of them which is called arguments, and this value contains all of the arguments that are passed through the function. So this time with the log args function, we haven't specified any named parameters. But we can still get access to any arguments which are passed when the function is invoked using the special arguments keyword. And you can see that arguments is an array like object and all of the individual arguments are available within this. You should be careful when working with arguments, even though it looks like an array, it's not a true array and so it doesn't have any of the methods that true arrays have. Another thing about functions that they always return a value. In the two functions that we've looked at so far, we haven't returned a value explicitly, so both of these functions actually return undefined. To specify what a function should return, we can use the return statement. So now, whenever the logArgs function is invoked, it will return the string some value. What the function returns depends likely on the purpose of the function. If the function is to compute some value, it should probably return this value. If the function fails to do what it should do, it could return false. As soon as the function returns, it stops executing completely. Another way that we can define functions is as a function expression. A function declaration always starts with the function keyword. So any expression which defines a function but doesn't start with the function keyword is a function expression. Function expressions are usually, but not always, functions that are stored in a variable. Like this. Function expressions largely behave in the same way as function declarations. We can still evoke them, but this time we used the variable name rather than the function identifier. In this case the function that we have saved in the variable f is known as an anonymous function because it doesn't have an identifier. Function expressions can also make use of functions that have identifiers. We just need to specify the identifier after the function keyword. So this time the function has the identifier f and it is also stored in the variable f. This can be useful for debugging purposes. So it is advised to try to always use identifiers even with function expressions. When a function expression has an identifier, like this, it's known as a named function expression. We can nest functions inside one another, we can pass functions to other functions, and functions can return functions. This means that JavaScript supports first class functions. Let's just spend a moment talking about hoisting. When your browser loads a JavaScript file there are two phases which occur. The browser first reads the entire file, this is known as the parsing phase. And only once it has parsed the entire file, does the browser execute the code in the file. This second phase is known as the execution phase. Variables are processed during the pausing phase, before any other code is executed. What this means is that even if we declare a variable halfway down a function, it will be available at the top of the function. JavaScript hoists it to the top. Let's look at a basic example. So we'll take a look at this in the browser's console in just a second. But before I switch back to the browser, what do you think the function is going to log? Let's take a quick look and see. You can see that it's logged undefined. Let's go back to the code and just talk through why. So what actually happened, when the browser loaded the index.js file, it first pass the entire file and in doing that it created any variables that were declared. So in this case the declared variable is test variable. So test variable was declared in the first phase, the parsing phase. The code is then executed in the second phase, the execution phase, and in this case the browser executes the code inside the file on a line by line basis from top to bottom. So when the test variable was declared in parsing phase, it was given the value undefined. The browser then executed the file and so the console.log statement appears before the value test is assigned to test variable. Because variable assignment happens in the execution phase, not the pausing phase. It is only the declaration of variables which happens in the parsing phase. So actually, it is as if we wrote the code like this. Even though we didn't write the code like that, that's how the browser interpreted it. Hoisting literally means that the code is lifted right to the top of the function. It's hoisted. So this only affects variables declared using the var keyword. Lets and consts do no exhibit the same behavior. Hoisting used to be problematic, especially for newcomers to Javascript. But these days, because it is recommended to use lets or consts most of the time, we can largely avoid these hosting issue. So in this lessons we looked at some basic facts about functions, such as how we can create them and invoke them, and how we can specify which parameters they accept. We also looked at how we can create functions that are invoked immediately. We also looked at the difference between function declarations and function expressions and variable hoisting. We'll now move on to the next section, which focuses on the data structures available to us in JavaScript. Thanks for watching.

Back to the top