Dennis
Dennis

Reputation: 8101

How can I execute (run) a JS function?

I did

var x = function (axis) {
    return 5;
};

alert(x);

and for output I got not 5, but

function (axis) {
    return 5;
};

what am I doing wrong?

Upvotes: 0

Views: 25

Answers (2)

Ivan Mushketyk
Ivan Mushketyk

Reputation: 8285

Try calling syntax:

<function_name>(<arguments>), so in your case:

x() 

or:

alert(x())

to display the output.

Upvotes: 4

Claudio Redi
Claudio Redi

Reputation: 68400

If you use x as parameter you're not executing the function and passing the result but passing the function itself.

To execute a function you need to add () at the end. So your first example should be

alert(x());

Upvotes: 4

Related Questions