As we all know, this following will not run the a() function so the alert box will not appear
// 1st
function a() {
alert('A!');
return function() {
alert('B!');
};
};
and we know that the following code will run the a() function and alert box 'A!' will appear
// 2nd
function a() {
alert('A!');
return function() {
alert('B!');
};
};
a(); // calling function
However, if we run following code, the a() function will be called and alert box 'A!' will also appear, just like the second code above
// 3rd
function a() {
alert('A!');
return function() {
alert('B!');
};
};
var x = a(); // assigning function to new variable
QUESTION: why does this happen (on 3rd snippet)? we didn't call the a() function yet (my current understanding). Didn't we just assigning x to a() function?.
You can work with functions as if they were objects. For example, you can assign functions to variables, to array elements, and to other objects. They can also be passed around as arguments to other functions or be returned from those functions. The only difference with objects is that functions can be called.
In Python, we can assign a function to a variable. And using that variable we can call the function as many as times we want. Thereby, increasing code reusability. Simply assign a function to the desired variable but without () i.e. just with the name of the function.
clone = function() { var newfun = new Function('return ' + this. toString())(); for (var key in this) newfun[key] = this[key]; return newfun; };
Assigning function to variable in JavaScript? Use return statement to assign function to variables. Following is the code − To run the above program, save the file name “anyName.html (index.html)” and right click on the file. Select the option “Open with Live Server” in VS Code editor.
The second method to assign the function to the variable is the arrow function. It is similar to the above approach, but the difference is that we will create an anonymous function without using the ‘ function ’ keyword and use an arrow instead.
You can assign the reference to a function held by one variable to another by omitting the parentheses. This can result in an easy-to-make mistake: attempting to assign the return value of a function to another variable, but accidentally assigning the reference to the function.
This tutorial teaches multiple variable assignments in JavaScript because variables are the most important part of our coding. Sometimes, we have to do many variable declarations and assignments as they have the same value. How? Let’s understand.
Didn't we just assigning x to a() function?.
No, you assigned the returned value from a() to x.
If you don't want to call a
, then do
var x = a;
and later do
x();
You're wrong. You are invoking the function:
var x = a(); // assigning function to new variable
This line of code says invoke a
when you write a()
. The parentheses indicate an invocation.
To assign a function to a variable you have to use just the name, such as:
var x = a;
or pass the name to a function f
:
f(a)
As a counter-example you invoke it in this next line of code and pass to g
not the function be the result of its execution:
g(a())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With