0
<html>
    <head>
        <script>
             function test(){                   
                 return function(){
                  alert("hi");
                }                
             }
             test();
        </script>
    </head>
    <body>
    </body>
</html>

This is my code, can I ask why it doesnt work properly??

3
  • What are you expecting it to do? Commented Jun 29, 2013 at 16:01
  • what do you want to return here? Commented Jun 29, 2013 at 16:05
  • i just want testing around javascript and learn how it works Commented Jun 29, 2013 at 16:10

2 Answers 2

11

Because you're returning your function but not invoking it. Try this:

test()();

Here is a fiddle

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

Comments

3

I think you might be confused. test() returns a function reference, but it won't execute it.

You could do something like this

var alertFunc = test(); // return function reference
alertFunc(); // call the function

1 Comment

this is better for sure ;)

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.