Function as variable in JavaScript

So, in the previous lesson, we found out that by referring to a function without parentheses, we get its source code.

In fact, everything is much more interesting. In JavaScript, unlike other languages, functions are just like the values of variables, just like numbers, strings, and arrays.

Let's look at an example. Suppose we have, for example, the function func:

function func() { console.log('!'); }

We don't actually have the function func. We have the variable func that stores the source code of the function.

We can, for example, overwrite the variable func with something else, such as a string. In this case, the function func will no longer be a function, but a string. Look at the example:

function func() { console.log('!'); } func(); // shows '!' func = 'string'; // overwrites the variable func console.log(func); // shows 'string'

Make the function func that will return some string through return.

Print the result of the function func with alert to the console.

Print the source code of the function func with alert to the console.

Write the number 123 into the variable func, thus overwriting the function in this variable. Print the new value of the variable func to the console.

enru