- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to define functions inside a function body in JavaScript?
To achieve what you want, use JavaScript Closures. A closure is a function, which uses the scope in which it was declared when invoked. It is not the scope in which it was invoked.
Example
Let’s take your example and this is how you can achieve your task. Here, innerDisplay() is a JavaScript closure.
Var myFunction = (function () { function display() { // 5 }; function innerDisplay (a) { if (/* some condition */ ) { // 1 // 2 display(); }else { // 3 // 4 display(); } } return innerDisplay; })();
- Related Articles
- How to define nested functions in JavaScript?
- How to define a function in JavaScript?
- How to call a Java function inside JavaScript Function?
- How to define getter and setter functions in JavaScript?
- How to define a JavaScript function using Function() Constructor?
- How to define global variable in a JavaScript function?
- How do we use function literal to define a function in JavaScript?
- How to define rest parameter for a function in JavaScript?
- How to define an Arrow Function in JavaScript?
- How to define custom sort function in JavaScript?
- How to define a JavaScript function using the expression?
- How to find the functions inside a package in R?
- How to run a function after two async functions complete - JavaScript
- How to define a function in Python?
- How to define a method in JavaScript?

Advertisements