1

In Javascript, I wanted to calculate total of two variables received from textbox, let us say txtBoxA and txtBoxB. The value gets calculated in an anonymous function and is stored in a variable total. Please take a look at the following code to see how it is calculated:

var total = function () {
            var total = parseFloat(txtbox[1].value) + parseFloat(txtbox[2].value);
            if (total == NaN) return 0;
            else return total;
        };
        alert(total);

but unfortunately, the anonymous function itself is printed as it is as shown in the image below. Anonymous Function Printed as text

1 Answer 1

4
alert(total);

You are just printing the function variable. If you want to execute it, you have to add (), so that it calls.

 alert(total());

And that is not an anonymous function. Just a function declaration and assigning to a variable.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot Suresh :) . It works Sorry got confused. I'll edit the question too ASAP
but bro, W3Schools said it was an anonymous function.. w3schools.com/js/js_function_definition.asp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.