Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Java by (13.1k points)

Can anyone help me with the functions that returns functions concept? I have these two code snippets:

function a() {

      

        alert('A!');

    

        function b(){

            alert('B!'); 

        }

    

        return b();

    }

    

    var s = a();

    alert('break');

    s();

Output:

A!

B!

break

Snippet 2:

function a() {

  

    alert('A!');

    function b(){

        alert('B!'); 

    }

    return b;

}

var s = a();

alert('break');

s();

Output:

A!

break

B!

Can anyone help me what is the difference between returning b and b()?

1 Answer

0 votes
by (26.7k points)

Basically, return the fucntion without bracs means it is returning the reference to the fucntion, whereas the function with () will going to execute the function and returns the value which is returned by the function.

// Execute function b() and return its value

return b();

// If b() has no return value, this is equivalent to calling b(), followed by

// return undefined;

I hope this will help.

Want to become a Java Expert? Join Java Certification now!!

Want to know more about Java? Watch this video on Java Course | Java Tutorial for Beginners:

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Mar 6, 2021 in Java by Jake (7k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...