Quiz
- When do you use var and let to declare variables?
The distinction is subtle and outside the scope of this course.
var
was the original and is still used.let
is newer and has some advantages in some subtle cases that won't concern us.You can use either one.
- Could you walk through another example of functions returning functions? / Can you explain more on the Functions as Return Values please? Going through the logic a bit.
Suppose we have a global list of employees. We might write some code to give them raises when needed.
Now, some examples:
- What is the purpose of an anonymous function / what are anonymous functions and how can they be used/when to use them? / More on anonymous functions.
Anonymous functions are great when you need a function but you don't want or can't name it.
- Could you give more examples of anonymous and named function side by side?
Sure. First, imagine a page with a buttons to go to "dark mode" or "light mode":
They might look like this:
Now, let's have an event handler:
- Could you provide more clarification on sections of callback functions and functions as return values?
There functions in the examples above are callback functions.
- Could we explain arrow functions a bit more?
Sure. An arrow function is a simplified syntax for anonymous functions. The following are (almost) the same:
The arrow function omits thefunction
keyword, the braces, and thereturn
keyword. -
A method similar to .map is forEach(), which takes a function of up to three arguments: the item, its index in the array, and the array itself. In practice, the third argument is often ignored, and we will omit it here. (Remember that JavaScript allows you to invoke a function with more arguments than it's declared to take. The extra arguments are just ignored. Here, if we supply an anonymous function that takes only two arguments, it means we aren't interested in the third argument — the array.)
In this part of the reading, you say that the third argument is ignored and omitted. Why so?
I said it's usually omitted. We omit it when we don't need it. Like the
giveRaise
function, above. Or:We would only need the third argument if we were in a context where we didn't know what the array was.