Menu

Post image 1
Post image 2
1 / 2
0

Closure function in JS

DEV Community·Nanthini Ammu·about 1 month ago
#8be5HPGd
Reading 0:00
15s threshold

JavaScript Nested Function : A function can also contain another function. This is called nested function. function greet ( name ) { function displayName () { console . log ( " Hi " + name ) } displayName (); } greet ( " Varun " ); Output : Hi Varun Enter fullscreen mode Exit fullscreen mode Returning a function : In JavaScript, it is allowed to return a function within a function. function greet ( name ) { function displayName () { console . log ( " Hi " + name ) } return displayName ; //returning a function } const g1 = greet ( " Varun " ); console . log ( g1 ); g1 (); Output : displayName () { console . log ( " Hi " + name ) } Hi Varun Enter fullscreen mode Exit fullscreen mode In the above program, the greet() function is returning the displayName function definition . The returned function definition is assigned to the g1 variable. When you print g1 using console.log(g1), you will get the function definition. To call the function stored in the g1 variable, we use g1() with parentheses.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More