Functions are powerful tools in any programming language, and JavaScript uses these tools in some flexible and creative ways. This chapter introduces a bit more of the power of functions.
We defined a value as "a specific piece of data." Some examples are the number 42
, the string "LC101"
, and the array ["MO", "FL", "DC"]
. Functions are also values, and while they appear to be very different from other values we have worked with, they share many core characteristics.
In particular, functions have a data type, just like all other values. Recall that a data type is a group of values that share characteristics, such as strings and numbers. Strings share the characteristics of having a length, while numbers don't. Numbers can be manipulated in ways that strings cannot, via operations like division and subtraction.
Example
The data type of the type conversion function Number
is function
. In fact, all functions are of type function
.
1console.log(typeof 42);
2console.log(typeof "LC101");
3console.log(typeof Number);
Console Output
number
string
function
Like other data types, functions may be assigned to variables. If we create a function named hello
, we can assign it to a variable with this syntax:
1function hello() {
2
3 // function body
4
5}
6
7let helloFunc = hello;
When a variable refers to a function, we can use the variable name to call the function:
helloFunc();
The variable helloFunc
can be thought of as an alias for the function hello
. When we call the function helloFunc
, JavaScript sees that it refers to the function hello
and calls that specific function.
When we use a variable name, we are really using its value. If the variable course
is assigned the value "LC101"
, then console.log(course)
prints "LC101"
. When a variable holds a function, it behaves the same way as when it holds a number or a string. The variable refers to the function.
Again, functions are values. They can be used just like general values. For example:
console.log
.Some of these function behaviors do not prove to be useful. You will probably never need to convert a function to a boolean, or ask whether a function is greater than 5. However, other behaviors, like passing functions as arguments and assigning them to variables, turn out to be extremely useful.