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.
Suppose we have a global list of employees. We might write some code to give them raises when needed.
Now, some examples:
Anonymous functions are great when you need a function but you don't want or can't name it.
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:
There functions in the examples above are callback functions.
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 the return
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.